-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
203 lines (168 loc) · 6.04 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import prompts from 'prompts';
import { getPackageManager, isValidPackageManager, validPackageManagers } from './lib/package';
import { getWorkspaces, CONFIGURATION } from './lib/workspace';
import { getConfigurationInfo, createConfiguration } from './lib/configuration';
import { runScript } from './lib/script';
import { hasPath, terminate, log, customChalk } from './lib/common';
import packageJson from './package.json';
import type { Configuration } from './types/configuration';
const { green, blue, cyan, yellow } = customChalk;
const { common, pnpm, lerna } = CONFIGURATION;
/**
* Notify if the current version is out of date.
*/
const notifyUpdate = async () => {
const { default: updateNotifier } = await import('update-notifier');
const notifier = updateNotifier({ pkg: packageJson });
if (notifier.update && notifier.update.latest !== packageJson.version) {
notifier.notify({
defer: false,
isGlobal: false,
message:
`New ${notifier.update.type} version available: ${chalk.dim(
'{currentVersion}',
)}${chalk.reset(' → ')}${
notifier.update.type === 'major'
? chalk.red('{latestVersion}')
: notifier.update.type === 'minor'
? chalk.yellow('{latestVersion}')
: chalk.green('{latestVersion}')
}\n` +
`Run ${chalk.cyan('{updateCommand}')} to update\n` +
`${chalk.dim.underline(`${packageJson.homepage ?? ''}/releases`)}`,
});
}
};
/**
* Command action: init
*/
export const init = async () => {
await notifyUpdate();
const styledCommand = green('mes init');
// Check which package manager.
const packageManager = getPackageManager();
if (packageManager === 'deno') {
terminate(`${green('deno')} is not supported.`);
}
const cwd = path.resolve(process.cwd());
// Check if CLI is running inside current working directory.
if (!cwd.startsWith(process.cwd())) {
terminate(`Run ${styledCommand} inside working directory.`);
}
// Check if the current working directory is the root directory.
if (!hasPath(cwd, '.git')) {
terminate(`Run ${styledCommand} in root directory.`);
}
// Check if package.json exists in current working directory.
if (!hasPath(cwd, 'package.json')) {
terminate(`Make sure that package.json exists in root directory.`);
}
// Check if the monorepo workspace information exists
const workspaces = getWorkspaces();
if (!workspaces) {
log('');
log('Could not found workspaces information in current working directory.');
log('');
log(`If using ${green('yarn')} or ${green('npm')},`);
log(`include ${blue(common.field)} field in root ${blue(common.file)}`);
log('');
log(`Else if using ${green('pnpm')},`);
log(`include ${blue(pnpm.field)} field in root ${blue(pnpm.file)}`);
log('');
log(`Else if using ${green('lerna')} and want to use configuration file of lerna,`);
log(`include ${blue(lerna.field)} field in root ${blue(lerna.file)}`);
terminate();
}
// Create configuration file in root directory.
log('');
log('> Collecting workspaces and scripts.');
const configurationInfo = getConfigurationInfo(packageManager, workspaces);
log('');
log(`> Creating configuration file.`);
createConfiguration(configurationInfo);
log('');
log(`Complete! Check ${yellow('.mesrc.json')}`);
};
/**
* Command action: run
*/
export const run = async () => {
const cwd = '.';
// Check if configuration file exists in root directory
if (!hasPath(cwd, '.mesrc.json')) {
log('');
log('Not found configuration file.');
log(`Run ${green('mes init')} first to create configuration file.`);
log('');
await init();
}
// Get configuration file from root
const configurationFile = fs.readFileSync('.mesrc.json', { encoding: 'utf-8' });
let { packageManager, workspaces, scripts }: Partial<Configuration> =
JSON.parse(configurationFile);
if (!packageManager) {
log('');
log(`Not found package manager.`);
log(`Apply ${cyan('npm')} by default.`);
log('');
packageManager = 'npm';
}
/**
* Check if configuration file has required information fully.
*/
if (typeof packageManager !== 'string') {
return terminate(
`${cyan('packageManager')} should be one of ${yellow(validPackageManagers.join(', '))}`,
);
}
if (!isValidPackageManager(packageManager) || packageManager === 'deno') {
log('');
log(`${green(packageManager)} is not supported.`);
log(`Supported package managers are ${yellow(validPackageManagers.join(', '))}.`);
return terminate();
}
if (!workspaces) {
return terminate(`Make sure ${green('workspaces')} field exists in configuration file.`);
}
if (!(workspaces instanceof Array)) {
return terminate(`Type of ${green('workspace')} should be ${yellow('Array<string>')}`);
}
if (!workspaces.length) {
return terminate(`workspaces should have one workspace at least.`);
}
if (!scripts) {
return terminate(`Make sure ${green('scripts')} field exists in configuration file.`);
}
// Select workspace
const { workspace } = await prompts(
{
type: 'select',
name: 'workspace',
message: `Which ${cyan('workspace')} would you like to run script?`,
initial: 0,
choices: workspaces.map(script => ({ title: script, value: script })),
},
{ onCancel: () => terminate('Exiting.') },
);
if (!scripts[workspace]) {
terminate(`${cyan(workspace)} doesn't exist in scripts as key.`);
} else if (!scripts[workspace].length) {
terminate(`Scripts of ${cyan(workspace)} don't have any script.`);
}
// Select workspace's script
const { script } = await prompts(
{
type: 'select',
name: 'script',
message: `Which ${cyan('script')} would you like to run?`,
initial: 0,
choices: scripts[workspace].map(script => ({ title: script, value: script })),
},
{ onCancel: () => terminate('Exiting.') },
);
// Run script
await runScript({ packageManager, workspace, script });
};