Skip to content

Commit 285cb2d

Browse files
Sync up NPM build script with graphql-js (#368)
1 parent b5d547d commit 285cb2d

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@
5858
"nyc": "15.1.0",
5959
"prettier": "2.4.0",
6060
"typescript": "4.4.2"
61+
},
62+
"publishConfig": {
63+
"tag": "latest"
6164
}
6265
}

resources/build.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ const path = require('path');
55
const assert = require('assert');
66

77
const babel = require('@babel/core');
8-
const prettier = require('prettier');
98

10-
const prettierConfig = JSON.parse(
11-
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
12-
);
13-
14-
const { readdirRecursive, showDirStats } = require('./utils');
9+
const {
10+
writeGeneratedFile,
11+
readdirRecursive,
12+
showDirStats,
13+
} = require('./utils');
1514

1615
if (require.main === module) {
1716
fs.rmSync('./npmDist', { recursive: true, force: true });
1817
fs.mkdirSync('./npmDist');
1918

19+
const packageJSON = buildPackageJSON();
20+
2021
const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
2122
for (const filepath of srcFiles) {
2223
const srcPath = path.join('./src', filepath);
@@ -38,11 +39,7 @@ if (require.main === module) {
3839
fs.copyFileSync('./README.md', './npmDist/README.md');
3940

4041
// Should be done as the last step so only valid packages can be published
41-
const packageJSON = buildPackageJSON();
42-
fs.writeFileSync(
43-
'./npmDist/package.json',
44-
JSON.stringify(packageJSON, null, 2),
45-
);
42+
writeGeneratedFile('./npmDist/package.json', JSON.stringify(packageJSON));
4643

4744
showDirStats('./npmDist');
4845
}
@@ -65,6 +62,10 @@ function buildPackageJSON() {
6562
delete packageJSON.scripts;
6663
delete packageJSON.devDependencies;
6764

65+
// TODO: move to integration tests
66+
const publishTag = packageJSON.publishConfig?.tag;
67+
assert(publishTag != null, 'Should have packageJSON.publishConfig defined!');
68+
6869
const { version } = packageJSON;
6970
const versionMatch = /^\d+\.\d+\.\d+-?(?<preReleaseTag>.*)?$/.exec(version);
7071
if (!versionMatch) {
@@ -74,20 +75,20 @@ function buildPackageJSON() {
7475
const { preReleaseTag } = versionMatch.groups;
7576

7677
if (preReleaseTag != null) {
77-
const [tag] = preReleaseTag.split('.');
78+
const splittedTag = preReleaseTag.split('.');
79+
// Note: `experimental-*` take precedence over `alpha`, `beta` or `rc`.
80+
const versionTag = splittedTag[2] ?? splittedTag[0];
7881
assert(
79-
tag.startsWith('experimental-') || ['alpha', 'beta', 'rc'].includes(tag),
80-
`"${tag}" tag is supported.`,
82+
['alpha', 'beta', 'rc'].includes(versionTag) ||
83+
versionTag.startsWith('experimental-'),
84+
`"${versionTag}" tag is not supported.`,
85+
);
86+
assert.equal(
87+
versionTag,
88+
publishTag,
89+
'Publish tag and version tag should match!',
8190
);
82-
83-
assert(!packageJSON.publishConfig, 'Can not override "publishConfig".');
84-
packageJSON.publishConfig = { tag: tag || 'latest' };
8591
}
8692

8793
return packageJSON;
8894
}
89-
90-
function writeGeneratedFile(filepath, body) {
91-
const formatted = prettier.format(body, { filepath, ...prettierConfig });
92-
fs.writeFileSync(filepath, formatted);
93-
}

resources/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const fs = require('fs');
44
const path = require('path');
55
const childProcess = require('child_process');
66

7+
const prettier = require('prettier');
8+
79
function exec(command, options) {
810
const output = childProcess.execSync(command, {
911
maxBuffer: 10 * 1024 * 1024, // 10MB
@@ -80,8 +82,18 @@ function showDirStats(dirPath) {
8082
);
8183
}
8284

85+
const prettierConfig = JSON.parse(
86+
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
87+
);
88+
89+
function writeGeneratedFile(filepath, body) {
90+
const formatted = prettier.format(body, { filepath, ...prettierConfig });
91+
fs.writeFileSync(filepath, formatted);
92+
}
93+
8394
module.exports = {
8495
exec,
8596
readdirRecursive,
8697
showDirStats,
98+
writeGeneratedFile,
8799
};

0 commit comments

Comments
 (0)