Skip to content

Commit

Permalink
fix: make sure copy file parent dir exits
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 2, 2025
1 parent e2321c7 commit 461094d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { writeFileSync, readFileSync, readdirSync, statSync, copyFileSync } from 'node:fs'
import { writeFileSync, readFileSync, readdirSync, statSync, copyFileSync, mkdirSync } from 'node:fs'
import path from 'node:path'

const cwd = process.cwd() + path.sep;
Expand Down Expand Up @@ -78,9 +78,11 @@ function copyFiles(baseDir: string) {
continue;
}
let targetFilepath = filepath.replace(sourceDir, commonjsDir);
mkdirSync(path.dirname(targetFilepath), { recursive: true });
copyFileSync(filepath, targetFilepath);
console.log('Copy %s to %s', filepath.replace(cwd, ''), targetFilepath.replace(cwd, ''));
targetFilepath = filepath.replace(sourceDir, esmDir);
mkdirSync(path.dirname(targetFilepath), { recursive: true });
copyFileSync(filepath, targetFilepath);
console.log('Copy %s to %s', filepath.replace(cwd, ''), targetFilepath.replace(cwd, ''));
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/demo/src/templates/bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
1 change: 1 addition & 0 deletions test/fixtures/demo/src/templates/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
9 changes: 9 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('test/index.test.ts', () => {
jsonFile = path.join(cwd, 'dist/esm/bar.json');
assert.equal(fs.statSync(jsonFile).isFile(), true);

let htmlFile = path.join(cwd, 'dist/commonjs/templates/foo.html');
assert.equal(fs.statSync(htmlFile).isFile(), true);
htmlFile = path.join(cwd, 'dist/esm/templates/foo.html');
assert.equal(fs.statSync(htmlFile).isFile(), true);
htmlFile = path.join(cwd, 'dist/commonjs/templates/bar.html');
assert.equal(fs.statSync(htmlFile).isFile(), true);
htmlFile = path.join(cwd, 'dist/esm/templates/bar.html');
assert.equal(fs.statSync(htmlFile).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'));
Expand Down

0 comments on commit 461094d

Please sign in to comment.