-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish.js
144 lines (123 loc) · 3.77 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
/* eslint-disable no-console */
import fs from 'fs-extra';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import inquirer from 'inquirer';
import crlf from 'crlf';
import glob from 'glob-promise';
import simpleGit from 'simple-git';
import runScript from './libs/run-script.js';
import logWelcome from './libs/log/welcome.js';
import logAbort from './libs/log/abort.js';
import logFinish from './libs/log/finish.js';
import spinner from './libs/spinner.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
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 git = simpleGit(__dirname);
const complete = () => {
waiting.stop();
spinner(title).succeed();
console.log(' ');
};
try {
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.')
complete();
else console.error(e);
}
};
const run = async () => {
logWelcome('Publish');
const defaultSelected = [
// 'koot',
// 'koot-webpack'
];
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: defaultSelected,
});
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));