Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto copy image/json/web files #2

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
Expand Down
42 changes: 39 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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() + path.sep;

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' + ')';
Expand All @@ -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);
3 changes: 3 additions & 0 deletions test/fixtures/demo/src/bar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
Binary file added test/fixtures/demo/src/config/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/fixtures/demo/src/config/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
16 changes: 0 additions & 16 deletions test/fixtures/demo/src/tsconfig.json

This file was deleted.

4 changes: 1 addition & 3 deletions test/fixtures/demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSources": true,
Expand All @@ -10,7 +8,7 @@
"moduleResolution": "nodenext",
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"skipLibCheck": false,
"sourceMap": true,
"strict": true,
"target": "es2022"
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Loading