forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish.js
103 lines (92 loc) · 2.7 KB
/
publish.js
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
95
96
97
98
99
100
101
102
103
'use strict';
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');
const reactScriptsPath = path.join(__dirname, 'packages/react-scripts');
const craTemplateCohtmlPath = path.join(
__dirname,
'packages/cra-template-cohtml'
);
const reactScriptsPackageJSON = getPackageJSON(reactScriptsPath);
const craTemplateCohtmlPackageJSON = getPackageJSON(craTemplateCohtmlPath);
/**
* Will get package json for some package
* @param {string} directory
* @returns {Object}
*/
function getPackageJSON(directory) {
const packageJSONPath = path.join(directory, 'package.json');
const fsStats = fs.lstatSync(packageJSONPath, { throwIfNoEntry: false });
if (!fsStats || !fsStats.isFile()) {
console.error(`Could not find package.json in ${directory}.`);
return null;
}
return JSON.parse(fs.readFileSync(packageJSONPath));
}
/**
* Gets the latest version of some npm package
* @param {string} npmPackage - The npm package name
* @returns {string}
*/
function getPublicVersion(npmPackage) {
return execSync(`npm view ${npmPackage} version`, {
encoding: 'utf8',
}).replace('\n', '');
}
/**
* Checks if some component should be updated in npm if its version is bumped
* @param {string} packageJSON
* @returns {boolean}
*/
function shouldUpdate(packageJSON) {
if (!packageJSON) {
return false;
}
const name = packageJSON.name;
console.log(`Checking if ${name} should be published...`);
// if a package doesn't exist in the registry then it must be published
if (
!JSON.parse(execSync(`npm search ${name} --json`, { encoding: 'utf8' }))
.length
) {
console.log(
`Package ${name} does not exist on the public registry - it should be published!`
);
return true;
}
const localVersion = packageJSON.version;
const publicVersion = getPublicVersion(name);
if (localVersion !== publicVersion) {
console.log(
`Package ${name} has new local version - ${localVersion}. The current npm version is ${publicVersion}.`
);
return true;
}
console.log(`Package ${name} needs no publish.`);
return false;
}
/**
* Publish a package to npm
* @param {string} name
* @param {string} directory
*/
function publish(name, directory) {
try {
execSync(`npm publish`, {
cwd: directory,
encoding: 'utf8',
stdio: 'inherit',
});
console.log(`Successfully published ${name}.`);
} catch (error) {
console.error(error);
}
}
module.exports = (() => {
if (shouldUpdate(reactScriptsPackageJSON)) {
publish(reactScriptsPackageJSON.name, reactScriptsPath);
}
if (shouldUpdate(craTemplateCohtmlPackageJSON)) {
publish(craTemplateCohtmlPackageJSON.name, craTemplateCohtmlPath);
}
})();