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