forked from alibaba/ice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.js
More file actions
117 lines (103 loc) · 3.08 KB
/
deploy.js
File metadata and controls
117 lines (103 loc) · 3.08 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { spawn } = require('child_process');
const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const npmRequestJson = require('npm-request-json');
const path = require('path');
const queue = require('queue');
//1. 创建 npmrc 文件
const cwd = process.cwd();
const npmrc = path.join(cwd, 'npmrc');
const NPM_EMAIL = process.env.NPM_EMAIL;
const NPM_TOKEN = process.env.NPM_TOKEN;
const NPM_REGISTRY = process.env.NPM_REGISTRY || 'registry.npmjs.com';
const registry = 'https://' + NPM_REGISTRY;
const npmrcContext = `email=${NPM_EMAIL}
registry=http://${NPM_REGISTRY}/
//${NPM_REGISTRY}/:_authToken=${NPM_TOKEN}
`;
fs.writeFile(npmrc, npmrcContext, function(error) {
if (error) {
console.error(error);
process.exit(1);
}
console.log('npmrc 文件创建成功');
scanMaterials();
});
function scanPackageJson(pattern) {
return new Promise((resolve, reject) => {
glob(pattern, { cwd: __dirname }, (error, files) => {
if (error) {
reject();
}
resolve(files.map((f) => path.join(__dirname, f)));
});
});
}
// 检测 npm 是否已发布
function checkNpmPublish(packagePath) {
const packageData = require(packagePath);
return npmRequestJson({
name: packageData.name,
version: packageData.version,
registry: registry,
})
.then((data) => {
return null;
})
.catch(() => {
console.log(chalk.red('未发布'), packageData.name, packageData.version);
return packagePath;
});
}
// 搜索所有物料文件
function scanMaterials() {
Promise.all([
scanPackageJson('../react-materials/*/*/package.json'),
scanPackageJson('../vue-materials/*/*/package.json'),
])
.then(([reactMaterials, vueMaterials = []]) => {
const allMaterials = [...reactMaterials, ...vueMaterials];
return Promise.all(allMaterials.map(checkNpmPublish));
})
.then((publishCheck) => {
const unpublished = publishCheck.filter((n) => !!n);
if (unpublished.length > 0) {
console.log(chalk.red('未发布的包:'), unpublished.length);
publishQueue(unpublished);
}
});
}
// npm publish 发布队列
function publishQueue(unpublishedPackageJson) {
const q = queue({
concurrency: 1, // 一次执行一个
});
unpublishedPackageJson.forEach((packageJson) => {
const publishCwd = path.dirname(packageJson);
q.push(function() {
return new Promise((resolve, reject) => {
const ps = spawn('npm', ['publish'], {
cwd: publishCwd,
stdio: 'inherit',
env: Object.assign({}, process.env, {
NPM_CONFIG_GLOBALCONFIG: npmrc, // 定义 npm 发布权限认证 rc 文件
}),
});
});
ps.on('close', (code) => {
if (code == 0) {
console.log(chalk.green('发布成功:'), packageJson);
resolve();
} else {
reject(new Error(packageJson + ' 发布失败'));
}
});
});
q.start();
q.end(function(err) {
if (err) throw err;
console.log(chalk.green('所有未发布的物料已发布完成'));
});
});
}