1+ const { execSync } = require ( 'child_process' ) ;
2+ const fs = require ( 'fs' ) ;
13const gulp = require ( 'gulp' ) ;
2- const shell = require ( 'gulp-shell' ) ;
4+ const path = require ( 'path' ) ;
5+ const process = require ( 'process' ) ;
36const ts = require ( 'gulp-typescript' ) ;
47
58const tsProject = ts . createProject ( 'tsconfig.json' ) ;
@@ -10,15 +13,88 @@ function clean(cb) {
1013 . then ( ( ) => cb ( ) ) ;
1114}
1215
16+ function sideload ( cb ) {
17+ if ( process . env . SECURITY_DEVOPS_ACTION_BUILD_SIDELOAD === 'true' ) {
18+ console . log ( 'Sideload mode enabled. Linking @microsoft/security-devops-actions-toolkit' ) ;
19+
20+ const toolkitSrcDir = path . resolve ( path . join ( __dirname , '..' , 'security-devops-actions-toolkit' ) ) ;
21+
22+ if ( ! fs . existsSync ( toolkitSrcDir ) ) {
23+ 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` ) ;
24+ }
25+
26+ const toolkitNodeModulesDir = path . join ( __dirname , 'node_modules' , '@microsoft' , 'security-devops-actions-toolkit' ) ;
27+
28+ if ( ! fs . existsSync ( toolkitNodeModulesDir ) ) {
29+ throw new Error ( `The node_modules directory for the toolkit does not exist. please run npm install before continuing: ${ toolkitNodeModulesDir } ` ) ;
30+ }
31+
32+ if ( process . env . SECURITY_DEVOPS_ACTION_BUILD_SIDELOAD_BUILD !== 'false' ) {
33+ console . log ( 'Building sideload project: npm run build' ) ;
34+ const output = execSync ( 'npm run build' , { cwd : toolkitSrcDir , encoding : 'utf8' } ) ;
35+ console . log ( output ) ;
36+ }
37+
38+ console . log ( `Clearing the existing toolkit directory: ${ toolkitNodeModulesDir } ` ) ;
39+ clearDir ( toolkitNodeModulesDir ) ;
40+
41+ const toolkitDistDir = path . join ( toolkitSrcDir , 'dist' ) ;
42+
43+ console . log ( "Copying sideload build..." ) ;
44+ copyFiles ( toolkitDistDir , toolkitNodeModulesDir ) ;
45+
46+ fs . writeFileSync (
47+ path . join ( toolkitNodeModulesDir , '.sideloaded' ) ,
48+ 'This package was built and sideloaded by the security-devops-action build process. Do not commit this file to source control.' ) ;
49+ }
50+ cb ( ) ;
51+ }
52+
1353function compile ( cb ) {
1454 tsProject
1555 . src ( )
1656 . pipe ( tsProject ( ) ) . js
17- . pipe ( gulp . dest ( 'lib' ) ) ;
18- cb ( ) ;
57+ . pipe ( gulp . dest ( 'lib' ) )
58+ . on ( 'end' , ( ) => cb ( ) ) ;
1959}
2060
61+ function clearDir ( dirPath ) {
62+ // Get a list of files and subdirectories in the directory
63+ const items = fs . readdirSync ( dirPath ) ;
64+
65+ for ( const item of items ) {
66+ const itemPath = path . join ( dirPath , item ) ;
67+
68+ if ( fs . statSync ( itemPath ) . isFile ( ) ) {
69+ fs . unlinkSync ( itemPath ) ;
70+ } else {
71+ clearDir ( itemPath ) ;
72+ }
73+ }
74+
75+ // Finally, remove the empty directory
76+ fs . rmdirSync ( dirPath ) ;
77+ }
78+
79+ function copyFiles ( srcDir , destDir ) {
80+ if ( ! fs . existsSync ( destDir ) ) {
81+ fs . mkdirSync ( destDir , { recursive : true } ) ;
82+ }
83+
84+ fs . readdirSync ( srcDir ) . forEach ( ( file ) => {
85+ const srcFilePath = path . join ( srcDir , file ) ;
86+ const destFilePath = path . join ( destDir , file ) ;
87+
88+ if ( fs . statSync ( srcFilePath ) . isDirectory ( ) ) {
89+ copyFiles ( srcFilePath , destFilePath ) ;
90+ } else {
91+ fs . copyFileSync ( srcFilePath , destFilePath ) ;
92+ console . log ( `Copied ${ srcFilePath } to ${ destFilePath } ` ) ;
93+ }
94+ } ) ;
95+ }
96+
2197exports . clean = clean ;
2298exports . compile = compile ;
23- exports . build = gulp . series ( clean , compile ) ;
99+ exports . build = gulp . series ( clean , sideload , compile ) ;
24100exports . default = exports . build ;
0 commit comments