-
Notifications
You must be signed in to change notification settings - Fork 47
/
gulpfile.js
111 lines (89 loc) · 3.85 KB
/
gulpfile.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
const { execSync } = require('child_process');
const fs = require('fs');
const gulp = require('gulp');
const path = require('path');
const process = require('process');
const ts = require('gulp-typescript');
const tsProject = ts.createProject('tsconfig.json');
const testTsProject = ts.createProject(path.join(__dirname, 'test', 'tsconfig.json'));
function clean(cb) {
import('del')
.then((del) => del.deleteSync(['lib']))
.then(() => cb());
}
function sideload(cb) {
if (process.env.SECURITY_DEVOPS_ACTION_BUILD_SIDELOAD === 'true') {
console.log('Sideload mode enabled. Linking @microsoft/security-devops-actions-toolkit');
const toolkitSrcDir = path.resolve(path.join(__dirname, '..', 'security-devops-actions-toolkit'));
if (!fs.existsSync(toolkitSrcDir)) {
throw new Error(`Could not the toolkit repo directory: ${toolkitSrcDir}. Please clone the repo to a parallel directory to this extension repo. Repo homepage: https://github.com/microsoft/security-devops-actions-toolkit`);
}
const toolkitNodeModulesDir = path.join(__dirname, 'node_modules', '@microsoft', 'security-devops-actions-toolkit');
if (!fs.existsSync(toolkitNodeModulesDir)) {
throw new Error(`The node_modules directory for the toolkit does not exist. please run npm install before continuing: ${toolkitNodeModulesDir}`);
}
if (process.env.SECURITY_DEVOPS_ACTION_BUILD_SIDELOAD_BUILD !== 'false') {
console.log('Building sideload project: npm run build');
const output = execSync('npm run build', { cwd: toolkitSrcDir, encoding: 'utf8' });
console.log(output);
}
console.log(`Clearing the existing toolkit directory: ${toolkitNodeModulesDir}`);
clearDir(toolkitNodeModulesDir);
const toolkitDistDir = path.join(toolkitSrcDir, 'dist');
console.log("Copying sideload build...");
copyFiles(toolkitDistDir, toolkitNodeModulesDir);
fs.writeFileSync(
path.join(toolkitNodeModulesDir, '.sideloaded'),
'This package was built and sideloaded by the security-devops-action build process. Do not commit this file to source control.');
}
cb();
}
function compile(cb) {
tsProject
.src()
.pipe(tsProject()).js
.pipe(gulp.dest('lib'))
.on('end', () => cb());
}
function compileTests(cb) {
testTsProject
.src()
.pipe(testTsProject()).js
.pipe(gulp.dest(path.join(__dirname, 'test')))
.on('end', () => cb());
}
function clearDir(dirPath) {
// Get a list of files and subdirectories in the directory
const items = fs.readdirSync(dirPath);
for (const item of items) {
const itemPath = path.join(dirPath, item);
if (fs.statSync(itemPath).isFile()) {
fs.unlinkSync(itemPath);
} else {
clearDir(itemPath);
}
}
// Finally, remove the empty directory
fs.rmdirSync(dirPath);
}
function copyFiles(srcDir, destDir) {
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
fs.readdirSync(srcDir).forEach((file) => {
const srcFilePath = path.join(srcDir, file);
const destFilePath = path.join(destDir, file);
if (fs.statSync(srcFilePath).isDirectory()) {
copyFiles(srcFilePath, destFilePath);
} else {
fs.copyFileSync(srcFilePath, destFilePath);
console.log(`Copied ${srcFilePath} to ${destFilePath}`);
}
});
}
exports.clean = clean;
exports.compile = compile;
exports.compileTests = compileTests;
exports.build = gulp.series(clean, sideload, compile);
exports.buildTests = gulp.series(exports.build, compileTests);
exports.default = exports.build;