|
| 1 | +import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const [source, destination, name] = process.argv.slice(2); |
| 5 | +if (!source || !destination || !name) { |
| 6 | + throw new Error( |
| 7 | + "Usage: prepare-npm-package.mjs SOURCE DESTINATION PACKAGE_NAME", |
| 8 | + ); |
| 9 | +} |
| 10 | + |
| 11 | +const root = path.resolve(import.meta.dirname, ".."); |
| 12 | +const sourceDirectory = path.resolve(root, source); |
| 13 | +const destinationDirectory = path.resolve(root, destination); |
| 14 | +const project = JSON.parse( |
| 15 | + await readFile(path.join(root, "package.json"), "utf8"), |
| 16 | +); |
| 17 | + |
| 18 | +await rm(destinationDirectory, { recursive: true, force: true }); |
| 19 | +await mkdir(destinationDirectory, { recursive: true }); |
| 20 | +await cp(sourceDirectory, destinationDirectory, { |
| 21 | + recursive: true, |
| 22 | + filter: (file) => !/[\\/]index\.html$|[\\/]_headers$/.test(file), |
| 23 | +}); |
| 24 | +const declaration = path.join(destinationDirectory, "types", "codeblocks.d.ts"); |
| 25 | +await writeFile( |
| 26 | + declaration, |
| 27 | + (await readFile(declaration, "utf8")).replace( |
| 28 | + 'import "./codeblocks.css";\n', |
| 29 | + "", |
| 30 | + ), |
| 31 | +); |
| 32 | +await writeFile( |
| 33 | + path.join(destinationDirectory, "package.json"), |
| 34 | + `${JSON.stringify( |
| 35 | + { |
| 36 | + name, |
| 37 | + version: project.version, |
| 38 | + description: project.description, |
| 39 | + license: project.license, |
| 40 | + type: "module", |
| 41 | + files: ["**/*"], |
| 42 | + exports: { |
| 43 | + ".": { |
| 44 | + types: "./types/codeblocks.d.ts", |
| 45 | + import: "./codeblocks-module.js", |
| 46 | + }, |
| 47 | + "./loader": { |
| 48 | + types: "./loader.d.ts", |
| 49 | + default: "./codeblocks.js", |
| 50 | + }, |
| 51 | + "./styles.css": "./codeblocks.css", |
| 52 | + }, |
| 53 | + }, |
| 54 | + null, |
| 55 | + 2, |
| 56 | + )}\n`, |
| 57 | +); |
0 commit comments