mirror of
https://github.com/chenasraf/simple-scaffold.git
synced 2026-05-18 01:29:09 +00:00
Compare commits
3 Commits
v1.7.0-dev
...
v1.1.3-pre
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
143519974f | ||
|
|
faa5788c38 | ||
|
|
7c8f4095ad |
6
.github/workflows/develop.yml
vendored
6
.github/workflows/develop.yml
vendored
@@ -2,7 +2,9 @@ name: Alpha Releases
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [develop]
|
||||
branches: [develop, fix/*, feature/*]
|
||||
# pull_request:
|
||||
# branches: [master, develop]
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -11,7 +13,7 @@ jobs:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: "12.x"
|
||||
node-version: "14.x"
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn test
|
||||
- run: yarn build
|
||||
|
||||
2
.github/workflows/docs.yml
vendored
2
.github/workflows/docs.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: "12.x"
|
||||
node-version: "14.x"
|
||||
- run: cd doc-theme && yarn install && yarn build && rm -rf node_modules && cd ..
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn typedoc
|
||||
|
||||
2
.github/workflows/pull_requests.yml
vendored
2
.github/workflows/pull_requests.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: "12.x"
|
||||
node-version: "14.x"
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn build
|
||||
- run: yarn test
|
||||
|
||||
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: "12.x"
|
||||
node-version: "14.x"
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn test
|
||||
- run: yarn build
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "simple-scaffold",
|
||||
"version": "1.1.2",
|
||||
"version": "1.1.3-pre.2",
|
||||
"description": "A simple command to generate any file structure, from single components to entire app boilerplates.",
|
||||
"homepage": "https://casraf.blog/simple-scaffold",
|
||||
"repository": "https://github.com/chenasraf/simple-scaffold.git",
|
||||
|
||||
22
src/utils.ts
22
src/utils.ts
@@ -136,6 +136,10 @@ export async function createDirIfNotExists(dir: string, config: ScaffoldConfig):
|
||||
}
|
||||
}
|
||||
|
||||
export function pathSepFix(pathname: string): string {
|
||||
return pathname.replace(/[\\\/]+/g, path.sep)
|
||||
}
|
||||
|
||||
export function getOptionValueForFile<T>(
|
||||
config: ScaffoldConfig,
|
||||
filePath: string,
|
||||
@@ -165,8 +169,8 @@ export function handlebarsParse(
|
||||
}
|
||||
const parser = Handlebars.compile(str, { noEscape: true })
|
||||
let outputContents = parser(data)
|
||||
if (isPath && path.sep !== "/") {
|
||||
outputContents = outputContents.replace(/\//g, "\\")
|
||||
if (isPath) {
|
||||
outputContents = pathSepFix(outputContents)
|
||||
}
|
||||
return Buffer.from(outputContents)
|
||||
} catch (e) {
|
||||
@@ -206,8 +210,7 @@ export function makeRelativePath(str: string): string {
|
||||
}
|
||||
|
||||
export function getBasePath(relPath: string): string {
|
||||
return path
|
||||
.resolve(process.cwd(), relPath)
|
||||
return pathSepFix(path.resolve(process.cwd(), relPath))
|
||||
.replace(process.cwd() + path.sep, "")
|
||||
.replace(process.cwd(), "")
|
||||
}
|
||||
@@ -257,12 +260,11 @@ export async function getTemplateFileInfo(
|
||||
config: ScaffoldConfig,
|
||||
{ templatePath, basePath }: { templatePath: string; basePath: string },
|
||||
): Promise<OutputFileInfo> {
|
||||
const inputPath = path.resolve(process.cwd(), templatePath)
|
||||
const outputPathOpt = getOptionValueForFile(config, inputPath, config.output)
|
||||
const outputDir = getOutputDir(config, outputPathOpt, basePath)
|
||||
const outputPath = handlebarsParse(config, path.join(outputDir, path.basename(inputPath)), {
|
||||
isPath: true,
|
||||
}).toString()
|
||||
const inputPath = pathSepFix(path.resolve(process.cwd(), templatePath))
|
||||
const outputPathOpt = pathSepFix(getOptionValueForFile(config, inputPath, config.output))
|
||||
const outputDir = pathSepFix(getOutputDir(config, outputPathOpt, basePath))
|
||||
const _rawOutputPath = pathSepFix(path.join(outputDir, path.basename(inputPath)))
|
||||
const outputPath = handlebarsParse(config, _rawOutputPath, { isPath: true }).toString()
|
||||
const exists = await pathExists(outputPath)
|
||||
return { inputPath, outputPathOpt, outputDir, outputPath, exists }
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { dateHelper, handlebarsParse, nowHelper } from "../src/utils"
|
||||
import { dateHelper, getTemplateFileInfo, handlebarsParse, nowHelper, pathSepFix } from "../src/utils"
|
||||
import { ScaffoldConfig } from "../src/types"
|
||||
import path from "path"
|
||||
import * as dateFns from "date-fns"
|
||||
@@ -12,16 +12,32 @@ const blankConf: ScaffoldConfig = {
|
||||
}
|
||||
|
||||
describe("Utils", () => {
|
||||
describe("getTemplateFileInfo", () => {
|
||||
mockPathSep()
|
||||
|
||||
test("should fix paths properly for windows", async () => {
|
||||
const result = await getTemplateFileInfo(
|
||||
{
|
||||
output: "/output",
|
||||
templates: ["/example/template/**/*"],
|
||||
name: "test",
|
||||
},
|
||||
{
|
||||
basePath: "/example/base",
|
||||
templatePath: "/example/template/file.ext",
|
||||
},
|
||||
)
|
||||
const wd = pathSepFix(process.cwd())
|
||||
expect(result).toHaveProperty("inputPath", "\\example\\template\\file.ext")
|
||||
expect(result).toHaveProperty("outputPathOpt", "\\output")
|
||||
expect(result).toHaveProperty("outputDir", "\\example\\base")
|
||||
expect(result).toHaveProperty("outputPath", "\\example\\base\\file.ext")
|
||||
})
|
||||
})
|
||||
|
||||
describe("handlebarsParse", () => {
|
||||
let origSep: any
|
||||
describe("windows paths", () => {
|
||||
beforeAll(() => {
|
||||
origSep = path.sep
|
||||
Object.defineProperty(path, "sep", { value: "\\" })
|
||||
})
|
||||
afterAll(() => {
|
||||
Object.defineProperty(path, "sep", { value: origSep })
|
||||
})
|
||||
mockPathSep()
|
||||
test("should work for windows paths", async () => {
|
||||
expect(handlebarsParse(blankConf, "C:\\exports\\{{name}}.txt", { isPath: true })).toEqual(
|
||||
Buffer.from("C:\\exports\\test.txt"),
|
||||
@@ -29,13 +45,6 @@ describe("Utils", () => {
|
||||
})
|
||||
})
|
||||
describe("non-windows paths", () => {
|
||||
beforeAll(() => {
|
||||
origSep = path.sep
|
||||
Object.defineProperty(path, "sep", { value: "/" })
|
||||
})
|
||||
afterAll(() => {
|
||||
Object.defineProperty(path, "sep", { value: origSep })
|
||||
})
|
||||
test("should work for non-windows paths", async () => {
|
||||
expect(handlebarsParse(blankConf, "/home/test/{{name}}.txt", { isPath: true })).toEqual(
|
||||
Buffer.from("/home/test/test.txt"),
|
||||
@@ -89,3 +98,19 @@ describe("Utils", () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function mockPathSep() {
|
||||
let origSep: any
|
||||
const sep = "\\"
|
||||
beforeAll(() => {
|
||||
origSep = path.sep
|
||||
Object.defineProperty(path, "sep", { value: sep })
|
||||
Object.defineProperty(path, "join", { value: (...args: string[]) => args.join(sep) })
|
||||
Object.defineProperty(path, "basename", { value: (arg: string) => arg.split(sep).slice(-1) })
|
||||
})
|
||||
afterAll(() => {
|
||||
Object.defineProperty(path, "sep", { value: origSep })
|
||||
Object.defineProperty(path, "join", { value: (...args: string[]) => args.join(origSep) })
|
||||
Object.defineProperty(path, "basename", { value: (arg: string) => arg.split(origSep).slice(-1) })
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user