-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (119 loc) · 3.42 KB
/
index.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
import os from 'os'
import fse from 'fs-extra'
import path from 'path'
import crypto from 'crypto'
import chalk from 'chalk'
import { quote } from 'shell-quote'
import { closest } from 'fastest-levenshtein'
import { findMonorepoRoot } from 'find-monorepo-root'
import { spawn } from './spawn.js'
async function ensureTargetDir(root) {
const nodeModules = path.join(root, 'node_modules')
const tmpdir = await fse.pathExists(nodeModules) ? nodeModules : os.tmpdir()
const targetDir = path.join(tmpdir, '.rss')
const exist = await fse.pathExists(targetDir)
if (!exist) {
await fse.mkdirp(targetDir)
}
return targetDir
}
function makeExecMain(cmd, args) {
const usedPos = []
const defaults = []
const main = cmd
// collect default values
.replace(/\{([1-9]\d?)=([^}]+)}/g, (whole, id, defaultValue) => {
const pos = id - 1
defaults[pos] = defaultValue
return `{${id}}`
})
// replace placeholder
.replace(/\{([*@]|[1-9]\d?)}/g, (whole, id) => {
if (id === "@" || id === '*') {
usedPos.push(...args.map((_, i) => i))
return id === '@' ? quote(args) : quote([args.join(' ')])
}
const pos = id - 1
if (pos >= 0 && pos < args.length) {
usedPos.push(pos)
const arg = args[pos]
if (arg && arg.trim()) {
return quote([arg])
}
}
if (defaults[pos] != null) {
return quote([defaults[pos]])
}
return ''
})
const others = args.filter((_, i) => !usedPos.includes(i))
return `${main} ${quote(others)}`
}
async function makeExecFile(root, task, main) {
const targetDir = await ensureTargetDir(root)
const win32 = process.platform === 'win32'
const ext = win32 ? '.cmd' : ''
const name = task.toLowerCase().replace(/[^0-9a-z]/g, '-')
let exist
let filePath
do {
const hash = crypto.randomBytes(16).toString('hex')
const filename = `${name}-${hash}${ext}`
filePath = path.join(targetDir, filename)
exist = await fse.pathExists(filePath)
} while (exist)
const declare = win32 ? '@ECHO OFF' : '#!/usr/bin/env sh'
await fse.writeFile(filePath, `${declare}\n\n${main}`)
await fse.chmod(filePath, 0o777)
return filePath
}
const rootCache = {}
async function findRoot() {
const cwd = process.cwd()
let root = rootCache[cwd]
if (root) {
return root
}
const { dir } = await findMonorepoRoot(cwd)
rootCache[cwd] = dir
return dir
}
function fixFilePath(root, file) {
if (path.isAbsolute(file)) {
return file
}
return path.resolve(root, file)
}
export async function run(task, args, options) {
const root = await findRoot()
const pkg = await fse.readJSON(path.join(root, 'package.json'))
const cmd = pkg.rss[task]
if (cmd) {
const isFile = typeof cmd === 'object' && cmd.file != null
const main = isFile ? cmd.file : makeExecMain(cmd, args)
let log = '> '
const eventColor = closest(task, [
'green',
'yellow',
'blue',
'magenta',
'cyan',
'red',
])
log += chalk[eventColor](`[${task}]`)
log += ' '
log += main
console.log(log)
if (!options.dryRun) {
const exe = isFile
? fixFilePath(root, main)
: await makeExecFile(root, task, main)
const child = spawn(exe, isFile ? args : [], { stdio: 'inherit' })
if (!isFile) {
child.on('exit', () => { fse.rm(exe) })
}
}
} else {
console.error(`unknown script: [${task}]`)
}
}