From dfc6f87acb26f6420f8c6dd1cdbad59d8251e20e Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 8 Jul 2024 12:27:39 +0800 Subject: [PATCH 1/3] feat: auto copy image/json/web files --- package.json | 4 +-- src/index.ts | 42 ++++++++++++++++++++-- test/fixtures/demo/src/bar.json | 3 ++ test/fixtures/demo/src/config/favicon.png | Bin 0 -> 6463 bytes test/fixtures/demo/src/config/foo.json | 3 ++ test/fixtures/demo/src/tsconfig.json | 16 --------- test/fixtures/demo/tsconfig.json | 4 +-- test/index.test.ts | 20 +++++++++++ 8 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 test/fixtures/demo/src/bar.json create mode 100644 test/fixtures/demo/src/config/favicon.png create mode 100644 test/fixtures/demo/src/config/foo.json delete mode 100644 test/fixtures/demo/src/tsconfig.json diff --git a/package.json b/package.json index 2c41b28..7ec2d67 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "@types/node": "^20.6.2", "coffee": "^5.5.0", "egg-bin": "^6.5.2", - "tshy": "^1.0.0" + "tshy": "^2.0.0" }, "type": "module", "tshy": { @@ -43,12 +43,10 @@ "./package.json": "./package.json", ".": { "import": { - "source": "./src/index.ts", "types": "./dist/esm/index.d.ts", "default": "./dist/esm/index.js" }, "require": { - "source": "./src/index.ts", "types": "./dist/commonjs/index.d.ts", "default": "./dist/commonjs/index.js" } diff --git a/src/index.ts b/src/index.ts index 0081460..563d8d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,19 @@ #!/usr/bin/env node -import { writeFileSync, readFileSync, readdirSync, statSync } from 'node:fs' +import { writeFileSync, readFileSync, readdirSync, statSync, copyFileSync } from 'node:fs' import path from 'node:path' -const cwd = process.cwd(); +const cwd = process.cwd() + '/'; const pkg = JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf-8')); // make sure commonjs *.d.ts can import types pkg.types = pkg.exports['.'].require.types; writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n') +writeFileSync('dist/package.json', JSON.stringify({ + name: pkg.name, + version: pkg.version, +}, null, 2) + '\n') // Replace all `( import.meta.url )` into `('import_meta_url_placeholder_by_tshy_after')` on commonjs. const IMPORT_META_URL = '(' + 'import.meta.url' + ')'; @@ -32,7 +36,39 @@ function replaceImportMetaUrl(baseDir: string) { continue; } writeFileSync(filepath, content.replaceAll(IMPORT_META_URL, IMPORT_META_URL_PLACE_HOLDER)); - console.log('Auto fix "import.meta.url" on %s', filepath); + console.log('Auto fix "import.meta.url" on %s', filepath.replace(cwd, '')); } } replaceImportMetaUrl(path.join(cwd, 'dist/commonjs')); + +// Copy image/json/web files +const fileExts = [ + '.jpg', '.jpeg', '.png', '.gif', '.webp', '.ico', + '.json', '.html', '.htm', '.css', +]; +const sourceDir = path.join(cwd, 'src'); +const commonjsDir = path.join(cwd, 'dist/commonjs'); +const esmDir = path.join(cwd, 'dist/esm'); + +function copyFiles(baseDir: string) { + const names = readdirSync(baseDir); + for (const name of names) { + const filepath = path.join(baseDir, name); + const stat = statSync(filepath); + if (stat.isDirectory()) { + copyFiles(filepath); + continue; + } + const extname = path.extname(filepath); + if (!fileExts.includes(extname)) { + continue; + } + let targetFilepath = filepath.replace(sourceDir, commonjsDir); + copyFileSync(filepath, targetFilepath); + console.log('Copy %s to %s', filepath.replace(cwd, ''), targetFilepath.replace(cwd, '')); + targetFilepath = filepath.replace(sourceDir, esmDir); + copyFileSync(filepath, targetFilepath); + console.log('Copy %s to %s', filepath.replace(cwd, ''), targetFilepath.replace(cwd, '')); + } +} +copyFiles(sourceDir); diff --git a/test/fixtures/demo/src/bar.json b/test/fixtures/demo/src/bar.json new file mode 100644 index 0000000..f2a886f --- /dev/null +++ b/test/fixtures/demo/src/bar.json @@ -0,0 +1,3 @@ +{ + "hello": "world" +} diff --git a/test/fixtures/demo/src/config/favicon.png b/test/fixtures/demo/src/config/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..5b8a2587de1b93d3b960b759fcc344e49e504e2f GIT binary patch literal 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z0llelykn1VEvNRM$~kY>6Sy zFvk0sy#S3XXttNw@o|iQBsPCZmlPYns-{_X@mf=*$t^5M+Q_ArHS6!w$I0!K^+Pzh z#CzMP>+0&31?5C<JlJ=?C5!t)*g&EFGR|AmDUFDh+w0OClfB;gozFuV0JjX(cokPBl?-w--~u! zxq}GDuD|qFBGFf1#1AADju^MNj5NebKBtSUrDI*4o)*d)22ZLU9U0+7c9hG2`RT;~ zFKkT>4X1v6APk$@TRMqLq_(o+%~cDmI6tG#ANx{Txx|@ij(FH=c71PeS!Iu<5ar_$ zp#Tpj$8||#VhD~~D^g#68*R$X0pUpUKl;#0bG%5$E4AjE#Os%;ML{UY>m*$&r=E&( z+aa#f$?BYWh0|%w>@jZ`d78V8V`2=*HpJVKam9Z${Y?XDb;1*^rG{v(%!#=<%pk$r zxF{;qaUrFGZ{M`qBkrhhZo~%AH;P=bzl?c8GT=(_0-=-KWME)W?H|U@&KXqg7wv6n z+Zct^!HIH|kL=irl5R??Tp}_0$osrlyi>*~mSzdp@^&Skv{l6gq$e;R9LW7jtudvQ zbz_m9$n(&L4yWV64@n{5qW>kDNxBmRo*40;lb8doaB_>*rU-J?{O?buMy~}ME0?OL zQJCW5V%n5DZ)|tj9#+*V`@porXP literal 0 HcmV?d00001 diff --git a/test/fixtures/demo/src/config/foo.json b/test/fixtures/demo/src/config/foo.json new file mode 100644 index 0000000..f2a886f --- /dev/null +++ b/test/fixtures/demo/src/config/foo.json @@ -0,0 +1,3 @@ +{ + "hello": "world" +} diff --git a/test/fixtures/demo/src/tsconfig.json b/test/fixtures/demo/src/tsconfig.json deleted file mode 100644 index b02cfc2..0000000 --- a/test/fixtures/demo/src/tsconfig.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "compilerOptions": { - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "inlineSources": true, - "jsx": "react", - "module": "nodenext", - "moduleResolution": "nodenext", - "noUncheckedIndexedAccess": true, - "resolveJsonModule": true, - "skipLibCheck": false, - "sourceMap": true, - "strict": true, - "target": "es2022" - } -} diff --git a/test/fixtures/demo/tsconfig.json b/test/fixtures/demo/tsconfig.json index 7f39495..b02cfc2 100644 --- a/test/fixtures/demo/tsconfig.json +++ b/test/fixtures/demo/tsconfig.json @@ -1,7 +1,5 @@ { "compilerOptions": { - "declaration": true, - "declarationMap": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "inlineSources": true, @@ -10,7 +8,7 @@ "moduleResolution": "nodenext", "noUncheckedIndexedAccess": true, "resolveJsonModule": true, - "skipLibCheck": true, + "skipLibCheck": false, "sourceMap": true, "strict": true, "target": "es2022" diff --git a/test/index.test.ts b/test/index.test.ts index c156b3d..8c3a6e3 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -37,5 +37,25 @@ describe('test/index.test.ts', () => { .end(); const pkg = JSON.parse(fs.readFileSync(packageFile, 'utf-8')); assert.equal(pkg.types, './dist/commonjs/index.d.ts'); + + // should copy image/json files + let pngFile = path.join(cwd, 'dist/commonjs/config/favicon.png'); + assert.equal(fs.statSync(pngFile).isFile(), true); + pngFile = path.join(cwd, 'dist/esm/config/favicon.png'); + assert.equal(fs.statSync(pngFile).isFile(), true); + let jsonFile = path.join(cwd, 'dist/commonjs/config/foo.json'); + assert.equal(fs.statSync(jsonFile).isFile(), true); + jsonFile = path.join(cwd, 'dist/esm/config/foo.json'); + assert.equal(fs.statSync(jsonFile).isFile(), true); + jsonFile = path.join(cwd, 'dist/commonjs/bar.json'); + assert.equal(fs.statSync(jsonFile).isFile(), true); + jsonFile = path.join(cwd, 'dist/esm/bar.json'); + assert.equal(fs.statSync(jsonFile).isFile(), true); + + // should dist/package.json exists, include name and version + const distPackageFile = path.join(cwd, 'dist/package.json'); + const distPkg = JSON.parse(fs.readFileSync(distPackageFile, 'utf-8')); + assert.equal(distPkg.name, 'tshy-after'); + assert.equal(distPkg.version, '1.0.0'); }); }); From d769d930667929a141acc282f27c6a4a9095ace5 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 8 Jul 2024 12:28:56 +0800 Subject: [PATCH 2/3] f --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 1b3ace9..8f7d65c 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,5 +12,5 @@ jobs: name: Node.js uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: - os: 'ubuntu-latest' + os: 'ubuntu-latest, windows-latest' version: '16, 18, 20, 22' From 2bbd13139c8dfda950a815055088a49ae55bec6d Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 8 Jul 2024 12:32:23 +0800 Subject: [PATCH 3/3] f --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 563d8d8..0161bf9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import { writeFileSync, readFileSync, readdirSync, statSync, copyFileSync } from 'node:fs' import path from 'node:path' -const cwd = process.cwd() + '/'; +const cwd = process.cwd() + path.sep; const pkg = JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf-8')); // make sure commonjs *.d.ts can import types