-
Notifications
You must be signed in to change notification settings - Fork 48.3k
/
Copy pathutils.js
128 lines (103 loc) · 2.91 KB
/
utils.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
'use strict';
const chalk = require('chalk');
const {exec} = require('child-process-promise');
const {existsSync, mkdirSync} = require('fs');
const {readJsonSync, writeJsonSync} = require('fs-extra');
const inquirer = require('inquirer');
const {join} = require('path');
const createLogger = require('progress-estimator');
const {
BUILD_METADATA_TEMP_DIRECTORY,
NPM_PACKAGES,
} = require('./configuration');
const logger = createLogger({
storagePath: join(__dirname, '.progress-estimator'),
});
async function checkNPMPermissions() {
const currentUser = await execRead('npm whoami');
const failedProjects = [];
const checkProject = async project => {
const owners = (await execRead(`npm owner ls ${project}`))
.split('\n')
.filter(owner => owner)
.map(owner => owner.split(' ')[0]);
if (!owners.includes(currentUser)) {
failedProjects.push(project);
}
};
await logger(
Promise.all(NPM_PACKAGES.map(checkProject)),
`Checking NPM permissions for ${chalk.bold(currentUser)}.`,
{estimate: 2500}
);
console.log('');
if (failedProjects.length) {
console.error(chalk.red.bold('Insufficient NPM permissions'));
console.error('');
console.error(
chalk.red(
`NPM user {underline ${currentUser}} is not an owner for: ${chalk.bold(
failedProjects.join(', ')
)}`
)
);
console.error(
chalk.red(
'Please contact a React team member to be added to the above project(s).'
)
);
process.exit(1);
}
}
function clear() {
console.clear();
}
async function confirm(message, exitFunction) {
console.log('');
const {confirmation} = await inquirer.prompt({
name: 'confirmation',
type: 'confirm',
message,
});
console.log('');
if (!confirmation) {
if (typeof exitFunction === 'function') {
exitFunction();
}
process.exit(0);
}
}
async function confirmContinue(exitFunction) {
await confirm('Continue the release?', exitFunction);
}
async function execRead(command, options) {
const {stdout} = await exec(command, options);
return stdout.trim();
}
function readSavedBuildMetadata() {
const path = join(BUILD_METADATA_TEMP_DIRECTORY, 'metadata');
if (!existsSync(path)) {
console.error(chalk.red('Expected to find build metadata at:'));
console.error(chalk.dim(` ${path}`));
process.exit(1);
}
const {archivePath, currentCommitHash} = readJsonSync(path);
return {archivePath, currentCommitHash};
}
function saveBuildMetadata({archivePath, currentCommitHash}) {
const path = join(BUILD_METADATA_TEMP_DIRECTORY, 'metadata');
if (!existsSync(BUILD_METADATA_TEMP_DIRECTORY)) {
mkdirSync(BUILD_METADATA_TEMP_DIRECTORY);
}
writeJsonSync(path, {archivePath, currentCommitHash}, {spaces: 2});
}
module.exports = {
checkNPMPermissions,
clear,
confirm,
confirmContinue,
execRead,
logger,
readSavedBuildMetadata,
saveBuildMetadata,
};