forked from hexojs/hexo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-readme.ts
168 lines (150 loc) · 5.87 KB
/
build-readme.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as croSpawn from 'cross-spawn';
import git from 'git-command-helper';
import minimist from 'minimist';
import nunjucks from 'nunjucks';
import picocolors from 'picocolors';
import { fs, path, writefile } from 'sbg-utility';
const parseWorkspaces = croSpawn
.async('yarn', ['workspaces', 'list', '--no-private', '--json'], {
cwd: process.cwd()
})
.then((o) =>
o.stdout
.split(/\r?\n/gm)
.filter((str) => str.length > 4)
.map((str) => {
const parse: { location: string; name: string } = JSON.parse(str.trim());
parse.location = path.join(__dirname, parse.location);
return parse;
})
.filter((o) => fs.existsSync(o.location))
);
/**
* is current device is Github Actions
*/
const _isCI = process.env.GITHUB_ACTION && process.env.GITHUB_ACTIONS;
const argv = minimist(process.argv.slice(2));
const gh = new git(__dirname, 'monorepo-v7');
export async function createReadMe() {
const workspaces = await parseWorkspaces;
if (Array.isArray(workspaces)) {
// set username and email on CI
if (_isCI) {
await croSpawn.async('git', ['config', '--global', 'user.name', 'dimaslanjaka'], {
cwd: __dirname,
stdio: 'inherit'
});
await croSpawn.async('git', ['config', '--global', 'user.email', '[email protected]'], {
cwd: __dirname,
stdio: 'inherit'
});
}
const readme = path.join(__dirname, 'releases/readme.md');
const source_readme = nunjucks.compile(fs.readFileSync(path.join(__dirname, 'build-readme.md'), 'utf-8'));
const source_vars = {
npm_prod: '',
npm_dev: '',
resolutions: '',
overrides: '',
yarn_prod: '',
yarn_dev: '',
commits: {}
};
const resolutions = {};
for (let i = 0; i < workspaces.length; i++) {
const workspace = workspaces[i];
const tarball = path.join(__dirname, 'releases', workspace.name + '.tgz');
if (!fs.existsSync(tarball)) {
console.log(tarball, picocolors.red('not found'));
continue;
}
const relativeTarball = path.toUnix(tarball.replace(__dirname, '')).replace(/^\//, '');
const checkIgnore = (await croSpawn.async('git', 'status --porcelain --ignored'.split(' '))).output
.split(/\r?\n/)
.map((str) => str.trim())
.filter((str) => str.startsWith('!!'))
.join('\n');
if (checkIgnore.includes(relativeTarball)) {
console.log(relativeTarball, picocolors.red('excluded by .gitignore'));
continue;
}
const workspaceGit = new git(workspace.location);
const commitURL = new URL(
(await workspaceGit.getremote()).push.url.replace(/.git$/, '') +
'/commit/' +
(await workspaceGit.latestCommit())
);
const cspl = commitURL.toString().split('/');
source_vars.commits[workspace.name] = `[${cspl[cspl.length - 1]}](${commitURL})`;
switch (workspace.name) {
case 'hexo':
source_vars.commits[workspace.name] +=
' [![Coverage Status](https://coveralls.io/repos/github/dimaslanjaka/hexo/badge.svg)](https://coveralls.io/github/dimaslanjaka/hexo)';
break;
default:
break;
}
const args = ['status', '--porcelain', '--', relativeTarball, '|', 'wc', '-l'];
const isChanged =
parseInt(
(
await croSpawn.async('git', args, {
cwd: __dirname,
shell: true
})
).output.trim()
) > 0;
console.log(
'git',
...args,
'->',
relativeTarball,
'is changed',
isChanged ? picocolors.green('true') : picocolors.gray('false')
);
/*if (isChanged) {
await croSpawn.async('git', ['add', relativeTarball], { cwd: __dirname, stdio: 'inherit' });
await croSpawn.async('git', ['commit', '-m', 'chore: update from ' + commitURL.pathname.replace(/^\/+/, '')], {
cwd: __dirname,
stdio: 'inherit'
});
}*/
// create installation
const tarballRawURL = (await gh.getGithubRepoUrl(relativeTarball)).rawURL;
const tarballProdURL = tarballRawURL.replace(
'monorepo-v7',
/*<production>*/ await gh.latestCommit(relativeTarball)
);
source_vars.npm_prod += `npm i ${workspace.name}@${tarballProdURL}\n`;
source_vars.npm_dev += `npm i ${workspace.name}@${tarballRawURL}\n`;
source_vars.yarn_dev += `yarn add ${workspace.name}@${tarballRawURL}\n`;
source_vars.yarn_prod += `yarn add ${workspace.name}@${tarballProdURL}\n`;
// create resolutions
resolutions[workspace.name] = tarballProdURL;
}
source_vars.resolutions = JSON.stringify({ name: 'your package name', resolutions }, null, 2);
source_vars.overrides = JSON.stringify({ name: 'your package name', overrides: resolutions }, null, 2);
source_vars.npm_prod = source_vars.npm_prod.trim();
source_vars.npm_dev = source_vars.npm_dev.trim();
let render = source_readme.render(source_vars);
if (argv['commits'] || argv['commit']) {
gh.add('releases');
const lc = await gh.latestCommit('releases');
const url = new URL('https://github.com/');
url.pathname =
new URL((await gh.getremote()).push.url.replace(/\.git$/, '')).pathname.replace(/^\//, '') + '/commit/' + lc;
await croSpawn
.async('git', ['commit', '-m', `chore(tarball): build ${String(url)}`], { cwd: gh.cwd })
.catch(() => console.log('cannot commit'));
}
// replace <production>
render = render.replace(/<production>/gm, await gh.latestCommit('releases'));
// await croSpawn.async('git', ['rev-list', '--parents', '-n', '1', await gh.latestCommit()], { cwd: __dirname }).stdout.trim().split(/\s/);
writefile(readme, render);
//await gh.add('releases/readme.md');
//await gh.commit('docs: update docs');
}
}
if (require.main === module) {
createReadMe();
}