Setting the file. One moment.
Import Readiness Contract · Wix Replatform · wix/skills · Skills Docs
ContentsBack to the top of the page lib/ import-readiness-contract.js
JavaScript · 59 lines · 4 KB
flatMap
(
entry
=>
entry.
isDirectory
()
?
files
(path.
join
(root, entry.name))
:
/
\.
[cm]
?
js
$
/
.
test
(entry.name)
?
[path.
join
(root, entry.name)]
:
[]);
7 }
8 // Blank only the file's header: whitespace, comments, a shebang and a 'use strict' directive
9 // (line comments end at any JS line terminator: LF, CR, U+2028, U+2029)
10 // before the first code token. Nothing can be open there, so this needs no guess about strings,
11 // templates or regex; comments after code are scanned as text like the code around them.
12 const LINE_END = / [\n\r \u2028\u2029 ] / g ;
13 function lineEnd ( text , from ) { LINE_END .lastIndex = from; return LINE_END . exec (text)?.index ?? text. length ; }
14 function blankHeaderComments ( text ) {
15 let i = text. startsWith ( '#!' ) ? lineEnd (text, 0 ) : 0 ;
16 for (;;) {
17 const rest = text. slice (i), space = rest. match ( / ^ \s + / ), directive = rest. match ( / ^ ( ['"] )use strict \1 ; ? / );
18 if (space || directive) i += (space || directive)[ 0 ]. length ;
19 else if (rest. startsWith ( '//' )) i = lineEnd (text, i);
20 else if (rest. startsWith ( '/*' ) && text. indexOf ( '*/' , i + 2 ) >= 0 ) i = text. indexOf ( '*/' , i + 2 ) + 2 ;
21 else break ;
22 }
23 return text. slice ( 0 , i). replace ( / [ ^ \n\r \u2028\u2029 ] / g , ' ' ) + text. slice (i);
24 }
25
26 // Vendored shared code lives in two trees: src/lib (project-owned runtime files and flat toolkit
27 // copies) and src/resources, which mirrors the toolkit's resources/ so cross-resource relative
28 // requires (resources -> ../../../lib) resolve to the one src/lib copy (rp-import-codegen layout).
29 // From the toolkit, a src/resources file is trusted only while byte-identical to the toolkit
30 // file at the same path; a vendored copy of this module cannot see the toolkit and trusts the
31 // path, as it trusts src/lib. import-contract-verify always runs from the toolkit before writes.
32 const TOOLKIT_ROOT = path. resolve (__dirname, '..' );
33 function vendoredResource ( src , file ) {
34 const relative = path. relative (src, file);
35 if ( ! / ^ resources [/ \\ ] / . test (relative)) return null ;
36 if ( ! fs. existsSync (path. join ( TOOLKIT_ROOT , 'SKILL.md' ))) return 'unmodified' ;
37 const toolkitFile = path. join ( TOOLKIT_ROOT , relative);
38 return fs. existsSync (toolkitFile) && fs. readFileSync (toolkitFile). equals (fs. readFileSync (file)) ? 'unmodified' : 'modified' ;
39 }
40 function verifyReadinessContract ( projectDir , descriptor ) {
41 const manifest = JSON . parse (fs. readFileSync (path. join (projectDir, 'execution/execution-manifest.json' ), 'utf8' ));
42 const expected = manifest.tasks. filter ( task => task.kind === 'import' ). map ( task => ({ taskId: task.id, edges: task.dependencyEdges. map (({ ref , mode }) => ({ ref, mode })). sort (( a , b ) => a.ref. localeCompare (b.ref)) })). sort (( a , b ) => a.taskId. localeCompare (b.taskId));
43 const reported = descriptor.dependencyReadiness?.tasks?. map ( task => ({ taskId: task.taskId, edges: task.edges. map (({ ref , mode }) => ({ ref, mode })). sort (( a , b ) => a.ref. localeCompare (b.ref)) })). sort (( a , b ) => a.taskId. localeCompare (b.taskId));
44 const errors = [];
45 if ( JSON . stringify (expected) !== JSON . stringify (reported)) errors. push ( 'dependency_readiness_descriptor_mismatch' );
46 const src = path. join (projectDir, 'src' );
47 for ( const file of files (src). filter ( file => ! / ^ (?:lib | extract) [/ \\ ] / . test (path. relative (src, file)))) {
48 const relative = path. relative (projectDir, file), vendored = vendoredResource (src, file);
49 if (vendored === 'unmodified' ) continue ;
50 const text = blankHeaderComments (fs. readFileSync (file, 'utf8' )), hits = [];
51 if ( / \b (?:startSetupRun | bindSetupStep | startQuickRun | startDevProbe | createWixSetupExecutor) \b| execution-capability(?: \. js) ? / . test (text)) hits. push ( 'execution_capability_bypass' );
52 // Generated transforms must obtain references through gate-owned resolvers, not read local evidence.
53 if ( /crosswalk | loadCrosswalk | readEntityCrosswalk | local-state | data [ \\ /] source-extract/ . test (text) && ! /(?: ^| \/ )run-import \. [cm] ? js $ / . test (relative)) hits. push ( 'dependency_reference_bypass' );
54 if (hits. length && vendored === 'modified' ) errors. push ( `vendored_resource_modified:${ relative }` );
55 else for ( const hit of hits) errors. push ( `${ hit }:${ relative }` );
56 }
57 return { ok: errors. length === 0 , errors };
58 }
59 module . exports = { verifyReadinessContract, files, blankHeaderComments };