forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-template-static-files.js
More file actions
125 lines (108 loc) · 3.84 KB
/
Copy pathbuild-template-static-files.js
File metadata and controls
125 lines (108 loc) · 3.84 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
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/quotes */
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-require-imports */
// @ts-check
// 这个文件是用来构建模板中的静态文件的
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const JSON5 = require('json5');
const { spawnSync } = require('child_process');
const PROJECT_ROOT = path.join(__dirname, '..');
const TEMPLATES = [
{
sourceDir: path.join(PROJECT_ROOT, 'static-files/rax'),
outputDir: path.join(PROJECT_ROOT, 'src/plugins/project/framework/rax/template'),
},
];
try {
TEMPLATES.forEach(buildTemplateStaticFiles);
console.log('All done.');
} catch (e) {
console.error(e);
process.exit(1);
}
function buildTemplateStaticFiles({ sourceDir, outputDir }) {
console.log('processing %s template...', path.dirname(sourceDir));
// 扫描所有的目录
const sourceFiles = glob.sync('**/*', {
nodir: true,
dot: true,
cwd: sourceDir,
});
console.log('got %d files: %o', sourceFiles.length, sourceFiles);
const staticFiles = {
imports: [],
runs: [],
};
// 生成对应的文件
sourceFiles.forEach((sourceFileName, index) => {
console.log('processing %s', sourceFileName);
const sourceFileContent = fs.readFileSync(path.join(sourceDir, sourceFileName), 'utf-8');
const sourceFileRealName = sourceFileName.replace(/\.template$/, '');
const outputFileName = `${sourceFileRealName}.ts`;
const outputFileFullPath = path.join(outputDir, 'files', outputFileName);
const sourceFileExtName = path.extname(sourceFileRealName);
const sourceFileBaseName = path.basename(sourceFileRealName, sourceFileExtName);
// 确保目录存在
fs.mkdirSync(path.dirname(outputFileFullPath), { recursive: true });
// 写入文件
fs.writeFileSync(
outputFileFullPath,
[
`/* eslint-disable max-len */`,
`/* Note: this file is generated by "npm run template", please dont modify this file directly */`,
`/* -- instead, you should modify "${path.relative(
PROJECT_ROOT,
path.join(sourceDir, sourceFileName),
)}" and run "npm run template" */`,
`import { ResultFile } from '@alilc/lowcode-types';`,
'',
`export default function getFile(): [string[], ResultFile] {`,
` return ${JSON5.stringify([
// 文件目录:
path.dirname(sourceFileRealName).split(path.sep).filter(Boolean),
// 文件名和内容:
{
name: sourceFileBaseName,
ext: sourceFileExtName.replace(/^\./, ''),
content: sourceFileContent,
},
])};`,
`}`,
'',
].join('\n'),
{
encoding: 'utf-8',
},
);
staticFiles.imports.push(`import file${index} from './files/${sourceFileRealName}';`);
staticFiles.runs.push(` runFileGenerator(root, file${index})`);
});
console.log('generating static-files.ts...');
fs.writeFileSync(
path.join(outputDir, 'static-files.ts'),
[
`/* Note: this file is generated by "npm run template", please dont modify this file directly */`,
`import { ResultDir } from '@alilc/lowcode-types';
import { createResultDir } from '../../../../../utils/resultHelper';
import { runFileGenerator } from '../../../../../utils/templateHelper';`,
...staticFiles.imports,
'',
`export function generateStaticFiles(root = createResultDir('.')): ResultDir {`,
...staticFiles.runs,
` return root;`,
`}`,
'',
].join('\n'),
{ encoding: 'utf-8' },
);
// prettier 一把
console.log('run prettier...');
spawnSync('npx', ['prettier', '--write', `${outputDir}`], {
stdio: 'inherit',
shell: true,
});
console.log('done %s', path.basename(sourceDir));
}