-
Notifications
You must be signed in to change notification settings - Fork 13
/
publish.js
151 lines (129 loc) · 3.98 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* eslint-disable no-console */
const fs = require('fs-extra');
const path = require('path');
const inquirer = require('inquirer');
const crlf = require('crlf');
const glob = require('glob-promise');
const runScript = require('./libs/run-script');
const logWelcome = require('./libs/log/welcome');
const logAbort = require('./libs/log/abort');
const logFinish = require('./libs/log/finish');
const spinner = require('./packages/koot/utils/spinner');
// ============================================================================
const defaultChecked = [
'create-koot-app',
'koot',
'koot-cli',
'koot-electron',
'koot-qiankun',
'koot-webpack',
];
// ============================================================================
const prePublish = async () => {
const title = 'pre-publish';
const waiting = spinner(title + '...');
const packages = await glob(
path.resolve(__dirname, 'packages/*/package.json')
);
const bins = [];
// 汇总所有 bin 文件
for (const packageJson of packages) {
const dir = path.dirname(packageJson);
const { bin = {} } = await fs.readJson(packageJson);
for (const pathname of Object.values(bin)) {
bins.push(path.resolve(dir, pathname));
}
}
// 将所有 bin 文件的换行符改为 LF
for (const file of bins) {
await new Promise((resolve, reject) => {
crlf.set(file, 'LF', (err, endingType) => {
if (err) return reject(err);
resolve(endingType);
});
});
}
// git commit
const complete = () => {
waiting.stop();
spinner(title).succeed();
console.log(' ');
};
try {
const git = require('simple-git')(__dirname);
const { modified = [] } = await git.status();
if (modified.length) {
await git.add('./*');
await git.commit(`changed all bins' line breaks to LF`);
}
complete();
} catch (e) {
if (
e.message === 'No staged files match any of provided globs.' ||
/No staged files found./.test(e.message)
)
complete();
else console.error(e);
}
};
const run = async () => {
logWelcome('Publish');
const dirPackages = path.resolve(__dirname, './packages');
const packages = (await fs.readdir(dirPackages)).filter((filename) => {
const dir = path.resolve(dirPackages, filename);
const lstat = fs.lstatSync(dir);
if (!lstat.isDirectory()) return false;
// 检查 package.json
const filePackage = path.resolve(dir, 'package.json');
if (!fs.existsSync(filePackage)) return false;
let p;
try {
p = fs.readJsonSync(filePackage);
} catch (e) {}
if (typeof p !== 'object') return false;
if (p.private) return false;
return true;
});
const { selected = [] } = await inquirer.prompt({
type: 'checkbox',
name: 'selected',
message: 'Select package(s) to publish\n ',
choices: packages,
default: defaultChecked,
});
console.log('');
if (!selected.length) {
logAbort('No package selected.');
return;
}
const { tag = false } = await inquirer.prompt({
type: 'list',
name: 'tag',
message: 'Select tag for NPM',
choices: [
{
name: 'Please select a tag',
value: false,
},
{
name: 'No tag (none)',
value: '',
},
'next',
],
default: 0,
});
console.log('');
if (tag === false) {
logAbort('No tag selected.');
return;
}
await prePublish();
await runScript(
`lerna publish` +
` --ignore-changes "packages/!(${selected.join('|')})/**"` +
(tag ? ` --dist-tag ${tag}` : '')
);
logFinish();
};
run().catch(async (e) => console.error(e));