Skip to content

Commit 6f86358

Browse files
committed
feat: add initial publish script
1 parent f484f0c commit 6f86358

6 files changed

+198
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules
22
*.swp
3+
/temp_bundle

.prettierrc.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"bracketSpacing": false,
3+
"printWidth": 80,
4+
"semi": true,
5+
"singleQuote": true,
6+
"tabWidth": 2,
7+
"trailingComma": "none",
8+
"useTabs": false,
9+
"arrowParens": "always"
10+
}

package-lock.json

+27-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "cjs-bundle",
33
"private": true,
4+
"type": "module",
45
"version": "0.0.1",
56
"description": "Creates a commonjs bundle of a given NPM package",
67
"main": "lib/main.js",
78
"scripts": {
8-
"test": "node --test"
9+
"test": "node --test",
10+
"format": "prettier --write \"scripts/*.js\""
911
},
1012
"repository": {
1113
"type": "git",
@@ -18,6 +20,8 @@
1820
},
1921
"homepage": "https://github.com/es-tooling/cjs-bundle#readme",
2022
"devDependencies": {
21-
"esbuild": "^0.20.0"
23+
"commander": "^12.0.0",
24+
"esbuild": "^0.20.0",
25+
"prettier": "^3.2.5"
2226
}
2327
}

scripts/create.js

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import {mkdir, readFile, writeFile, rm} from 'node:fs/promises';
2+
import * as esbuild from 'esbuild';
3+
import {program} from 'commander';
4+
import {promisify} from 'node:util';
5+
import {exec} from 'node:child_process';
6+
import {fileURLToPath} from 'node:url';
7+
import {join as joinPath} from 'node:path';
8+
9+
const execAsyncNative = promisify(exec);
10+
11+
const execAsync = async (command, opts) => {
12+
const result = execAsyncNative(command, opts);
13+
const [output] = await Promise.all([
14+
result,
15+
new Promise((resolve) => {
16+
result.child.on('close', () => {
17+
resolve(result);
18+
});
19+
})
20+
]);
21+
return output;
22+
};
23+
24+
const rootDir = fileURLToPath(new URL('..', import.meta.url));
25+
26+
const packageKeysToKeep = [
27+
'version',
28+
'description',
29+
'repository',
30+
'author',
31+
'license',
32+
'bugs',
33+
'homepage',
34+
'engines'
35+
];
36+
37+
async function createBundle(opts) {
38+
const packageTemplatePath = joinPath(
39+
rootDir,
40+
'./scripts/package-template.json'
41+
);
42+
const packageTemplate = JSON.parse(
43+
await readFile(packageTemplatePath, {encoding: 'utf8'})
44+
);
45+
const name = opts.name.replaceAll(/[^@\w]/g, '');
46+
const bundleDirName = 'temp_bundle';
47+
const cwd = joinPath(rootDir, bundleDirName);
48+
const actualPackagePath = joinPath(
49+
rootDir,
50+
bundleDirName,
51+
'node_modules',
52+
name,
53+
'package.json'
54+
);
55+
const bundlePath = joinPath(cwd, 'main.js');
56+
57+
packageTemplate.name = `@cjs-bundle/${name}`;
58+
packageTemplate.type = 'module';
59+
packageTemplate.devDependencies[name] = 'latest';
60+
61+
console.log('Removing any existing bundle directory');
62+
await rm(`./${bundleDirName}`, {recursive: true, force: true});
63+
64+
console.log('Creating bundle directory');
65+
await mkdir(`./${bundleDirName}`);
66+
67+
console.log('Creating package.json');
68+
await writeFile(
69+
`./${bundleDirName}/package.json`,
70+
JSON.stringify(packageTemplate, null, 2),
71+
{encoding: 'utf8'}
72+
);
73+
74+
console.log('Running `npm i`');
75+
await execAsync('npm i', {
76+
cwd
77+
});
78+
79+
const actualPackage = JSON.parse(
80+
await readFile(actualPackagePath, {encoding: 'utf8'})
81+
);
82+
83+
console.log('Copying package.json fields across');
84+
for (const key of packageKeysToKeep) {
85+
packageTemplate[key] = actualPackage[key];
86+
}
87+
88+
delete packageTemplate.type;
89+
delete packageTemplate.devDependencies[name];
90+
91+
console.log('Writing new package.json');
92+
await writeFile(
93+
`./${bundleDirName}/package.json`,
94+
JSON.stringify(packageTemplate, null, 2),
95+
{encoding: 'utf8'}
96+
);
97+
98+
console.log('Bundling');
99+
await esbuild.build({
100+
bundle: true,
101+
absWorkingDir: cwd,
102+
stdin: {contents: `export * from 'chai';`, resolveDir: cwd},
103+
format: 'cjs',
104+
outfile: bundlePath
105+
});
106+
107+
console.log('Running `npm pkg fix`');
108+
await execAsync('npm pkg fix', {cwd});
109+
110+
console.log('Publishing');
111+
const publishCommand = opts.dryRun ? 'npm publish --dry-run' : 'npm publish';
112+
const publishResult = await execAsync(publishCommand, {cwd});
113+
114+
console.log(publishResult.stderr);
115+
console.log(publishResult.stdout);
116+
}
117+
118+
program.option('--name <name>').option('--dry-run');
119+
120+
program.parse();
121+
122+
const options = program.opts();
123+
124+
if (options.name) {
125+
const dryRun = options.dryRun ?? false;
126+
createBundle({name: options.name, dryRun});
127+
}

scripts/package-template.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "main.js",
6+
"files": [
7+
"./main.js"
8+
],
9+
"exports": {
10+
".": "./main.js"
11+
},
12+
"scripts": {},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/es-tooling/cjs-bundle.git"
16+
},
17+
"author": "James Garbutt (https://github.com/43081j)",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/es-tooling/cjs-bundle/issues"
21+
},
22+
"homepage": "https://github.com/es-tooling/cjs-bundle#readme",
23+
"dependencies": {},
24+
"devDependencies": {
25+
"esbuild": "^0.20.0"
26+
}
27+
}

0 commit comments

Comments
 (0)