soinproduction-gutenberg 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ import path from 'node:path';
4
4
  import {fileURLToPath} from 'node:url';
5
5
  import {syncSections} from './sync-sections.mjs';
6
6
  import {syncKit} from './sync-kit.mjs';
7
+ import {normalizeSourceImports} from './materialize-source.mjs';
7
8
  const source=path.resolve(fileURLToPath(new URL('..',import.meta.url)));
8
9
  const command=process.argv[2]??'help';
9
10
  const args=process.argv.slice(3);
@@ -20,6 +21,7 @@ if(command==='prepare'){
20
21
  const meta=JSON.parse(fs.readFileSync(path.join(source,'package.json'),'utf8'));
21
22
  fs.writeFileSync(marker,JSON.stringify({name:meta.name,version:meta.version},null,2)+'\n');
22
23
  }
24
+ normalizeSourceImports(destination);
23
25
  syncSections(project);
24
26
  syncKit(project,{directory:destination});
25
27
  console.log('Prepared component/editor/PHP/style connections in '+destination);
@@ -0,0 +1,25 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import ts from 'typescript';
4
+ /** The distributed ESM uses .js paths; a host TS source copy must resolve TS files. */
5
+ export function normalizeSourceImports(directory){
6
+ const walk=folder=>fs.readdirSync(folder,{withFileTypes:true}).flatMap(entry=>entry.isDirectory()?walk(path.join(folder,entry.name)):[path.join(folder,entry.name)]);
7
+ for(const file of walk(directory).filter(file=>/\.tsx?$/.test(file))){
8
+ let text=fs.readFileSync(file,'utf8');
9
+ const source=ts.createSourceFile(file,text,ts.ScriptTarget.Latest,true),edits=[];
10
+ const visit=node=>{
11
+ let specifier;
12
+ if((ts.isImportDeclaration(node)||ts.isExportDeclaration(node))&&node.moduleSpecifier&&ts.isStringLiteral(node.moduleSpecifier))specifier=node.moduleSpecifier;
13
+ if(ts.isCallExpression(node)&&node.expression.kind===ts.SyntaxKind.ImportKeyword&&ts.isStringLiteral(node.arguments[0]))specifier=node.arguments[0];
14
+ if(ts.isImportTypeNode(node)&&ts.isLiteralTypeNode(node.argument)&&ts.isStringLiteral(node.argument.literal))specifier=node.argument.literal;
15
+ if(specifier&&specifier.text.startsWith('.')&&specifier.text.endsWith('.js')){
16
+ const base=path.resolve(path.dirname(file),specifier.text.slice(0,-3));
17
+ if(['.ts','.tsx'].some(extension=>fs.existsSync(base+extension)))edits.push({start:specifier.getStart(source)+1,end:specifier.getEnd()-1,value:specifier.text.slice(0,-3)});
18
+ }
19
+ ts.forEachChild(node,visit);
20
+ };
21
+ visit(source);
22
+ for(const edit of edits.sort((a,b)=>b.start-a.start))text=text.slice(0,edit.start)+edit.value+text.slice(edit.end);
23
+ if(edits.length)fs.writeFileSync(file,text);
24
+ }
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soinproduction-gutenberg",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
package/tooling/cli.mjs CHANGED
@@ -4,6 +4,7 @@ import path from 'node:path';
4
4
  import {fileURLToPath} from 'node:url';
5
5
  import {syncSections} from './sync-sections.mjs';
6
6
  import {syncKit} from './sync-kit.mjs';
7
+ import {normalizeSourceImports} from './materialize-source.mjs';
7
8
  const source=path.resolve(fileURLToPath(new URL('..',import.meta.url)));
8
9
  const command=process.argv[2]??'help';
9
10
  const args=process.argv.slice(3);
@@ -20,6 +21,7 @@ if(command==='prepare'){
20
21
  const meta=JSON.parse(fs.readFileSync(path.join(source,'package.json'),'utf8'));
21
22
  fs.writeFileSync(marker,JSON.stringify({name:meta.name,version:meta.version},null,2)+'\n');
22
23
  }
24
+ normalizeSourceImports(destination);
23
25
  syncSections(project);
24
26
  syncKit(project,{directory:destination});
25
27
  console.log('Prepared component/editor/PHP/style connections in '+destination);
@@ -0,0 +1,25 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import ts from 'typescript';
4
+ /** The distributed ESM uses .js paths; a host TS source copy must resolve TS files. */
5
+ export function normalizeSourceImports(directory){
6
+ const walk=folder=>fs.readdirSync(folder,{withFileTypes:true}).flatMap(entry=>entry.isDirectory()?walk(path.join(folder,entry.name)):[path.join(folder,entry.name)]);
7
+ for(const file of walk(directory).filter(file=>/\.tsx?$/.test(file))){
8
+ let text=fs.readFileSync(file,'utf8');
9
+ const source=ts.createSourceFile(file,text,ts.ScriptTarget.Latest,true),edits=[];
10
+ const visit=node=>{
11
+ let specifier;
12
+ if((ts.isImportDeclaration(node)||ts.isExportDeclaration(node))&&node.moduleSpecifier&&ts.isStringLiteral(node.moduleSpecifier))specifier=node.moduleSpecifier;
13
+ if(ts.isCallExpression(node)&&node.expression.kind===ts.SyntaxKind.ImportKeyword&&ts.isStringLiteral(node.arguments[0]))specifier=node.arguments[0];
14
+ if(ts.isImportTypeNode(node)&&ts.isLiteralTypeNode(node.argument)&&ts.isStringLiteral(node.argument.literal))specifier=node.argument.literal;
15
+ if(specifier&&specifier.text.startsWith('.')&&specifier.text.endsWith('.js')){
16
+ const base=path.resolve(path.dirname(file),specifier.text.slice(0,-3));
17
+ if(['.ts','.tsx'].some(extension=>fs.existsSync(base+extension)))edits.push({start:specifier.getStart(source)+1,end:specifier.getEnd()-1,value:specifier.text.slice(0,-3)});
18
+ }
19
+ ts.forEachChild(node,visit);
20
+ };
21
+ visit(source);
22
+ for(const edit of edits.sort((a,b)=>b.start-a.start))text=text.slice(0,edit.start)+edit.value+text.slice(edit.end);
23
+ if(edits.length)fs.writeFileSync(file,text);
24
+ }
25
+ }