-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish-script.js
More file actions
94 lines (79 loc) · 2.53 KB
/
publish-script.js
File metadata and controls
94 lines (79 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';
// Imports
const path = require('path');
const fs = require('fs');
const util = require('util');
const child_process = require('child_process');
const fsp = {
readFile: util.promisify(fs.readFile),
writeFile: util.promisify(fs.writeFile),
};
// Configuration
const ROOT_DIR = __dirname;
const DIST_DIR = path.join(ROOT_DIR, './dist/');
const NPM_COMMAND = osIsWindows() ? 'npm.cmd' : 'npm';
const PACKAGE_JSON_PATH = path.join(ROOT_DIR, './package.json');
const README_PATH = path.join(ROOT_DIR, './README.md');
///////////////////////////////////////////////////////////
publishScript();
async function publishScript() {
await runBuildProcess();
await Promise.all([
copyPackageJsonToDist(),
copyReadmeToDist(),
]);
console.log('Build process finished successfully, spawning publish process . . .');
await spawnPublishDistProcess();
console.log('Build publish finished successfully!');
}
async function copyPackageJsonToDist() {
// Get package.json contents
const packageJsonContents = await fsp.readFile(PACKAGE_JSON_PATH);
const originalPkg = JSON.parse(packageJsonContents.toString());
// Remove the `private` option
delete originalPkg.private;
// Convert back to a formatted JSON string.
const fixedPkg = JSON.stringify(originalPkg, null, 2);
// Create fixed `package.json` in the dist directory.
await fsp.writeFile(path.join(DIST_DIR, './package.json'), fixedPkg);
}
async function copyReadmeToDist() {
const readmeContents = await fsp.readFile(README_PATH);
await fsp.writeFile(path.join(DIST_DIR, './README.md'), readmeContents);
}
async function runBuildProcess() {
return new Promise((resolve, reject) => {
const buildProcess = child_process.spawn(NPM_COMMAND, ['run', 'build'], {
cwd: ROOT_DIR,
stdio: 'inherit',
});
buildProcess.on('close', code => {
if (code !== 0) {
reject(new Error(`ERROR! Build process could not complete, exited with error code ${code}.`));
return;
}
resolve();
});
});
}
async function spawnPublishDistProcess() {
return new Promise((resolve, reject) => {
const publishProcess = child_process.spawn(NPM_COMMAND, ['publish'], {
cwd: DIST_DIR,
stdio: 'inherit',
});
publishProcess.on('close', code => {
if (code !== 0) {
reject(`ERROR! Publish process could not complete, exited with error code ${code}.`);
return;
}
resolve();
});
});
}
///////////////////////////////////////////////////////////
// Utilities
///////////////////////////////////////////////////////////
function osIsWindows() {
return process.platform === 'win32';
}