generated from react-component/trigger
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.ts
76 lines (62 loc) · 2.41 KB
/
run.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
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import c from 'picocolors'
import { CHECK, WARN, PREFIX } from './constants'
import { nanoid } from 'nanoid'
import execa from 'execa';
export interface CliOptions {
/**
* @default `antd.min.css`
*/
output?: string;
input?: string;
overwrite?: boolean;
}
const tsxPath = require.resolve('tsx/cli');
const coreFilePath = path.join(__dirname, '..');
export async function run(options: CliOptions = {}) {
const { output = "antd.min.css", input = '', overwrite } = options;
const OVERWRITE = !!process.env.ALLAY_OVERWRITE /** Can be used for CI */ || overwrite;
const cwd = process.cwd()
const outputFilePath = path.join(cwd, output);
const inputFilePath = path.join(cwd, input);
const userInputFileExits = input.length > 0 && fs.existsSync(inputFilePath)
if (input.length > 0 && !userInputFileExits) {
console.log(c.yellow(`${WARN} ${input.length ? c.bold(input) : 'input'} is not exists.`));
}
if (fs.existsSync(outputFilePath) && !OVERWRITE) {
throw new Error(`${output} is already exists.`);
}
const outputDir = path.dirname(outputFilePath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// ====== Generate temp file ======
const id = nanoid().replaceAll('-', '_');
const fileExt = userInputFileExits ? path.extname(inputFilePath) : '.js';
const internalFileName = `.temp_${PREFIX}_${id}${fileExt}`,
internalVariable = `${PREFIX}_${id}`;
let internalFileContent = '';
if (userInputFileExits) {
internalFileContent += `import ${internalVariable} from ${JSON.stringify(inputFilePath)};\n`
} else {
internalFileContent += `const ${internalVariable} = void 0;\n`
}
internalFileContent += `
import { extractStyle } from ${JSON.stringify(coreFilePath)};
import fs from 'fs';
fs.writeFileSync(${JSON.stringify(outputFilePath)}, extractStyle(${internalVariable}));
`.trim();
const tmpFilePath = path.join(os.tmpdir(), internalFileName);
const symlinkPath = path.join(path.dirname(inputFilePath), internalFileName);
fs.writeFileSync(tmpFilePath, internalFileContent, { encoding: 'utf-8', mode: 0o777 });
fs.symlinkSync(tmpFilePath, symlinkPath, 'file');
execa.node(tsxPath, [symlinkPath])
.then(() => {
console.log(c.green(`${CHECK} ${c.bold(output)} is generated.`));
})
.finally(() => {
fs.unlinkSync(symlinkPath);
})
}