-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-task-on-workspaces.cjs
More file actions
executable file
·133 lines (106 loc) · 5.11 KB
/
run-task-on-workspaces.cjs
File metadata and controls
executable file
·133 lines (106 loc) · 5.11 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
/* eslint-disable header/header */
if (process.argv.length < 3) {
console.log('You have to specify what workspace task to run on all')
console.log('\nUsage: run-task-on-workspaces [task] [arguments]');
console.log('\nExamples of tasks: build|test|ci');
process.exit(1);
}
const path = require('path');
const fs = require('fs');
const spawn = require('child_process').spawnSync;
const editJsonFile = require('edit-json-file');
const rootPackageJson = require('./package.json')
const glob = require('glob').sync;
const workspaces = {};
const distFolder = `dist${path.sep}`
for (const workspaceDef of rootPackageJson.workspaces) {
console.log(`Getting packages for workspace definition '${workspaceDef}' \n`);
const files = glob(workspaceDef, { cwd: process.cwd() });
const packages = files
.filter(_ => path.basename(_) === 'package.json' && _.indexOf(distFolder) < 0 && _.indexOf('node_modules') < 0)
.sort((a, b) => a.length - b.length);
packages.forEach(_ => {
const localPackage = JSON.parse(fs.readFileSync(_).toString());
workspaces[localPackage.name] = path.dirname(_);
console.log(`Including workspace '${localPackage.name}' at '${workspaces[localPackage.name]}'`);
});
}
console.log('');
const task = process.argv[2];
const args = process.argv.slice(3, process.argv.length);
console.log(`Performing '${task}' on workspaces`);
if (args.length > 0) {
console.log(` Using args : ${args}`);
}
console.log('');
const workspaceNames = Object.keys(workspaces);
function updateDependencyVersionsFromLocalWorkspaces(file, packageJson, version) {
const dependencyFields = Object.keys(packageJson).filter(_ => _.endsWith('dependencies') || _.endsWith('Dependencies'));
for (let field of dependencyFields) {
let actualField = field;
const dependencies = packageJson[field] ?? {};
const fileDependencies = file.get(field);
for (let dependencyName of Object.keys(dependencies)) {
if (workspaceNames.includes(dependencyName)) {
console.log(`Updating workspace ${field} '${dependencyName}' to version ${version}`);
const actualDependencyName = dependencyName.replace(/\./g, '\\.');
actualField = field.replace(/\./g, '\\.');
fileDependencies[actualDependencyName] = version;
}
}
file.set(actualField, fileDependencies);
}
}
for (const workspaceName in workspaces) {
const workspaceRelativeLocation = workspaces[workspaceName];
const workspaceAbsoluteLocation = path.join(process.cwd(), workspaceRelativeLocation);
const packageJsonFile = path.join(workspaceAbsoluteLocation, 'package.json');
if (fs.existsSync(packageJsonFile)) {
const file = editJsonFile(packageJsonFile, { stringify_width: 4 });
const packageJson = file.toObject();
if (packageJson.private === true) {
console.log(`Workspace private '${workspaceName}' at '${workspaceRelativeLocation}'`);
continue;
}
if (task === 'publish-version') {
if (args.length === 1) {
const version = args[0];
file.set('version', version);
updateDependencyVersionsFromLocalWorkspaces(file, packageJson, version);
file.save();
const targetReadMe = path.join(workspaceAbsoluteLocation, 'README.md');
if (!fs.existsSync(targetReadMe)) {
fs.copyFileSync(path.join(process.cwd(), "README.md"), targetReadMe);
}
console.log(`Publishing workspace '${workspaceName}' at '${workspaceRelativeLocation}'`);
const result = spawn('npm', ['publish'], { cwd: workspaceAbsoluteLocation, shell: true });
console.log(result.stdout.toString());
if (result.status !== 0) {
console.log(`Error publishing workspace '${workspaceName}'`);
process.exit(1);
}
}
} else {
if (!packageJson.scripts || !packageJson.scripts.hasOwnProperty(task)) {
console.log(`Skipping workspace '${workspaceName}' - no script with name '${task}'`);
continue;
}
console.log(`Workspace '${workspaceName}' at '${workspaceRelativeLocation}'`);
const result = spawn('yarn', [task], { cwd: workspaceAbsoluteLocation, shell: true });
if (result.error) {
console.log(`Error running task '${task}' on workspace '${workspaceName}':`,
result.error.message);
console.error(result.error.stack);
process.exit(1);
}
console.log(result.stdout.toString());
if (result.status !== 0) {
console.log(`Error running task '${task}' on workspace '${workspaceName}'`);
console.log(result.stderr.toString());
process.exit(1);
}
}
}
}