Skip to content

Commit 564e821

Browse files
committed
maintain directory structure
1 parent a52f9a0 commit 564e821

5 files changed

Lines changed: 23 additions & 13 deletions

File tree

examples/test-input/Component/inner/inner-{{name}}.txt

Whitespace-only changes.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"dev": "tsc --watch",
1616
"start": "node dist/scaffold.js",
1717
"test": "jest --verbose",
18-
"cmd": "node dist/cmd.js",
18+
"cmd": "node --trace-warnings dist/cmd.js",
1919
"build-test": "yarn build && yarn test",
2020
"build-cmd": "yarn build && yarn cmd",
2121
"prepack": "yarn build"

src/scaffold.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,42 @@ export async function Scaffold(config: ScaffoldConfig) {
3131
for (let template of config.templates) {
3232
try {
3333
// try {
34-
const tplStat = await stat(template)
35-
if (tplStat.isDirectory()) {
34+
const _isDir = await isDir(template)
35+
const basePath = path
36+
.resolve(process.cwd(), _isDir ? template : path.dirname(template.replace("*", "").replace("//", "/")))
37+
.replace(process.cwd(), ".")
38+
if (_isDir) {
3639
template = template + "/**/*"
3740
}
38-
// } catch (e) {
41+
// } catch (e: any) {
3942
// if (e.code !== "ENOENT") {
4043
// throw e
4144
// }
4245
// }
43-
const files = await promisify(glob)(template, { dot: true })
46+
const files = await promisify(glob)(template, { dot: true, debug: false })
4447
for (const templatePath of files) {
45-
await handleTemplateFile(templatePath, options, data)
48+
if (!(await isDir(templatePath))) {
49+
await handleTemplateFile(templatePath, basePath, options, data)
50+
}
4651
}
47-
} catch (e) {
52+
} catch (e: any) {
4853
handleErr(e)
4954
}
5055
}
51-
} catch (e) {
56+
} catch (e: any) {
5257
console.error(e)
5358
throw e
5459
}
5560
}
5661

62+
async function isDir(path: string): Promise<boolean> {
63+
const tplStat = await stat(path)
64+
return tplStat.isDirectory()
65+
}
66+
5767
async function handleTemplateFile(
5868
templatePath: string,
69+
basePath: string,
5970
options: ScaffoldConfig,
6071
data: Record<string, string>
6172
): Promise<void> {
@@ -92,7 +103,7 @@ async function handleTemplateFile(
92103
log(options, `File ${outputPath} already exists, skipping`)
93104
}
94105
resolve()
95-
} catch (e) {
106+
} catch (e: any) {
96107
handleErr(e)
97108
reject(e)
98109
}

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function log(options: ScaffoldConfig, ...obj: any[]) {
2929
if (options.quiet) {
3030
return
3131
}
32-
console.log(...obj)
32+
console["log"](...obj)
3333
}
3434

3535
export async function createDirIfNotExists(dir: string, options: ScaffoldConfig): Promise<void> {
@@ -43,7 +43,7 @@ export async function createDirIfNotExists(dir: string, options: ScaffoldConfig)
4343
try {
4444
await mkdir(dir)
4545
return
46-
} catch (e) {
46+
} catch (e: any) {
4747
if (e.code !== "EEXIST") {
4848
throw e
4949
}
@@ -78,7 +78,7 @@ export async function pathExists(filePath: string): Promise<boolean> {
7878
try {
7979
await access(filePath, F_OK)
8080
return true
81-
} catch (e) {
81+
} catch (e: any) {
8282
if (e.code === "ENOENT") {
8383
return false
8484
}

tests/scaffold.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ describe("Scaffold", () => {
180180
})
181181

182182
const dir = readdirSync(process.cwd())
183-
console.log({ dir })
184183
expect(dir).toHaveProperty("length")
185184
})
186185

0 commit comments

Comments
 (0)