1- import { chmodSync , existsSync , writeFileSync } from 'node:fs'
1+ import {
2+ chmodSync ,
3+ copyFileSync ,
4+ existsSync ,
5+ readFileSync ,
6+ writeFileSync
7+ } from 'node:fs'
28import path from 'node:path'
39
410import { toSortedObject } from '@socketsecurity/registry/lib/objects'
@@ -26,15 +32,88 @@ const {
2632 rootSrcPath
2733} = constants
2834
35+ const CONSTANTS_JS = 'constants.js'
36+
2937const distModuleSyncPath = path . join ( rootDistPath , 'module-sync' )
3038const distRequirePath = path . join ( rootDistPath , 'require' )
3139
3240const binBasenames = [ 'cli.js' , 'npm-cli.js' , 'npx-cli.js' ]
3341const editablePkgJson = readPackageJsonSync ( rootPath , { editable : true } )
3442
35- function setBinPerm ( filepath ) {
36- // Make file chmod +x.
37- chmodSync ( filepath , 0o755 )
43+ function copyConstantsModuleSync ( srcPath , destPath ) {
44+ copyFileSync (
45+ path . join ( srcPath , CONSTANTS_JS ) ,
46+ path . join ( destPath , CONSTANTS_JS )
47+ )
48+ }
49+
50+ function modifyConstantsModuleExportsSync ( distPath ) {
51+ const filepath = path . join ( distPath , CONSTANTS_JS )
52+ let code = readFileSync ( filepath , 'utf8' )
53+ code = code
54+ // Remove @babel /runtime helpers from code.
55+ . replace (
56+ / f u n c t i o n g e t D e f a u l t E x p o r t F r o m C j s [ \s \S ] + ?c o n s t a n t s \$ \d + \. d e f a u l t = v o i d 0 ; ? \n / ,
57+ ''
58+ )
59+ // Remove @babel /runtime and @rollup /commonjs interop from code.
60+ . replace ( / ^ (?: e x p o r t s .[ $ \w ] + | [ $ \w ] + \. d e f a u l t ) \s * = .* (?: \n | $ ) / gm, '' )
61+ code = code + 'module.exports = constants\n'
62+ writeFileSync ( filepath , code , 'utf8' )
63+ }
64+
65+ function rewriteConstantsModuleSync ( distPath ) {
66+ writeFileSync (
67+ path . join ( distPath , CONSTANTS_JS ) ,
68+ `'use strict'\n\nmodule.exports = require('../constants.js')\n` ,
69+ 'utf8'
70+ )
71+ }
72+
73+ function setBinPermsSync ( distPath ) {
74+ for ( const binBasename of binBasenames ) {
75+ // Make file chmod +x.
76+ chmodSync ( path . join ( distPath , binBasename ) , 0o755 )
77+ }
78+ }
79+
80+ function updateDepStatsSync ( depStats ) {
81+ const { content : pkgJson } = editablePkgJson
82+ const oldDepStats = existsSync ( depStatsPath )
83+ ? readJsonSync ( depStatsPath )
84+ : undefined
85+ Object . assign ( depStats . dependencies , {
86+ // Manually add @cyclonedx /cdxgen and synp as they are not directly
87+ // referenced in the code but used through spawned processes.
88+ '@cyclonedx/cdxgen' : pkgJson . dependencies [ '@cyclonedx/cdxgen' ] ,
89+ synp : pkgJson . dependencies . synp ,
90+ // Assign old dep stats dependencies to preserve them.
91+ ...oldDepStats ?. dependencies
92+ } )
93+ // Remove transitives from dependencies.
94+ for ( const key of Object . keys ( oldDepStats ?. transitives ?? { } ) ) {
95+ if ( pkgJson . dependencies [ key ] ) {
96+ depStats . transitives [ key ] = pkgJson . dependencies [ key ]
97+ depStats . external [ key ] = pkgJson . dependencies [ key ]
98+ delete depStats . dependencies [ key ]
99+ }
100+ }
101+ depStats . dependencies = toSortedObject ( depStats . dependencies )
102+ depStats . devDependencies = toSortedObject ( depStats . devDependencies )
103+ depStats . esm = toSortedObject ( depStats . esm )
104+ depStats . external = toSortedObject ( depStats . external )
105+ depStats . transitives = toSortedObject ( depStats . transitives )
106+ // Write dep stats.
107+ writeFileSync ( depStatsPath , `${ formatObject ( depStats ) } \n` , 'utf8' )
108+ // Update dependencies with additional inlined modules.
109+ editablePkgJson
110+ . update ( {
111+ dependencies : {
112+ ...depStats . dependencies ,
113+ ...depStats . transitives
114+ }
115+ } )
116+ . saveSync ( )
38117}
39118
40119export default ( ) => {
@@ -76,9 +155,10 @@ export default () => {
76155 plugins : [
77156 {
78157 writeBundle ( ) {
79- for ( const binBasename of binBasenames ) {
80- setBinPerm ( path . join ( distModuleSyncPath , binBasename ) )
81- }
158+ setBinPermsSync ( distModuleSyncPath )
159+ copyConstantsModuleSync ( distModuleSyncPath , rootDistPath )
160+ modifyConstantsModuleExportsSync ( rootDistPath )
161+ rewriteConstantsModuleSync ( distModuleSyncPath )
82162 }
83163 }
84164 ]
@@ -104,46 +184,9 @@ export default () => {
104184 plugins : [
105185 {
106186 writeBundle ( ) {
107- const { content : pkgJson } = editablePkgJson
108- const oldDepStats = existsSync ( depStatsPath )
109- ? readJsonSync ( depStatsPath )
110- : undefined
111- const { depStats } = requireConfig . meta
112- Object . assign ( depStats . dependencies , {
113- // Manually add @cyclonedx /cdxgen and synp as they are not directly
114- // referenced in the code but used through spawned processes.
115- '@cyclonedx/cdxgen' : pkgJson . dependencies [ '@cyclonedx/cdxgen' ] ,
116- synp : pkgJson . dependencies . synp ,
117- // Assign old dep stats dependencies to preserve them.
118- ...oldDepStats ?. dependencies
119- } )
120- // Remove transitives from dependencies.
121- for ( const key of Object . keys ( oldDepStats ?. transitives ?? { } ) ) {
122- if ( pkgJson . dependencies [ key ] ) {
123- depStats . transitives [ key ] = pkgJson . dependencies [ key ]
124- depStats . external [ key ] = pkgJson . dependencies [ key ]
125- delete depStats . dependencies [ key ]
126- }
127- }
128- depStats . dependencies = toSortedObject ( depStats . dependencies )
129- depStats . devDependencies = toSortedObject ( depStats . devDependencies )
130- depStats . esm = toSortedObject ( depStats . esm )
131- depStats . external = toSortedObject ( depStats . external )
132- depStats . transitives = toSortedObject ( depStats . transitives )
133- // Write dep stats.
134- writeFileSync ( depStatsPath , `${ formatObject ( depStats ) } \n` , 'utf8' )
135- // Update dependencies with additional inlined modules.
136- editablePkgJson
137- . update ( {
138- dependencies : {
139- ...depStats . dependencies ,
140- ...depStats . transitives
141- }
142- } )
143- . saveSync ( )
144- for ( const binBasename of binBasenames ) {
145- setBinPerm ( path . join ( distRequirePath , binBasename ) )
146- }
187+ setBinPermsSync ( distRequirePath )
188+ rewriteConstantsModuleSync ( distRequirePath )
189+ updateDepStatsSync ( requireConfig . meta . depStats )
147190 }
148191 }
149192 ]
0 commit comments