Compare commits

...

1 Commits

Author SHA1 Message Date
Chen Asraf
0ecf07de18 accept async beforeWrite callback 2022-04-19 22:25:10 +03:00
4 changed files with 59 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "simple-scaffold",
"version": "1.1.0-alpha.2",
"version": "1.1.0-alpha.3",
"description": "Create files based on templates",
"repository": "https://github.com/chenasraf/simple-scaffold.git",
"author": "Chen Asraf <inbox@casraf.com>",

View File

@@ -130,7 +130,11 @@ export interface ScaffoldConfig {
* @returns `String | Buffer | undefined` The final output of the file contents-only, after further modifications -
* or `undefined` to use the original content (i.e. `content.toString()`)
*/
beforeWrite?(content: Buffer, rawContent: Buffer, outputPath: string): string | Buffer | undefined
beforeWrite?(
content: Buffer,
rawContent: Buffer,
outputPath: string
): string | Buffer | undefined | Promise<string | Buffer | undefined>
}
export interface ScaffoldCmdConfig {
name: string

View File

@@ -299,7 +299,7 @@ export async function copyFileTransformed(
const templateBuffer = await readFile(inputPath)
const unprocessedOutputContents = handlebarsParse(options, templateBuffer)
const finalOutputContents = (
options.beforeWrite?.(unprocessedOutputContents, templateBuffer, outputPath) ?? unprocessedOutputContents
(await options.beforeWrite?.(unprocessedOutputContents, templateBuffer, outputPath)) ?? unprocessedOutputContents
).toString()
if (!options.dryRun) {

View File

@@ -94,7 +94,7 @@ describe("Scaffold", () => {
verbose: 0,
})
const data = readFileSync(join(process.cwd(), "output", "app_name.txt"))
expect(data.toString()).toBe("Hello, my app is app_name")
expect(data.toString()).toEqual("Hello, my app is app_name")
})
test("should create with config", async () => {
@@ -107,7 +107,7 @@ describe("Scaffold", () => {
})
const data = readFileSync(join(process.cwd(), "output", "app_name", "app_name.txt"))
expect(data.toString()).toBe("Hello, my app is app_name")
expect(data.toString()).toEqual("Hello, my app is app_name")
})
})
)
@@ -133,7 +133,7 @@ describe("Scaffold", () => {
})
const data = readFileSync(join(process.cwd(), "output", "app_name.txt"))
expect(data.toString()).toBe("Hello, my value is 1")
expect(data.toString()).toEqual("Hello, my value is 1")
})
test("should overwrite with config", async () => {
@@ -155,7 +155,7 @@ describe("Scaffold", () => {
})
const data = readFileSync(join(process.cwd(), "output", "app_name.txt"))
expect(data.toString()).toBe("Hello, my value is 2")
expect(data.toString()).toEqual("Hello, my value is 2")
})
})
)
@@ -188,6 +188,33 @@ describe("Scaffold", () => {
})
)
describe(
"dry run",
withMock(fileStructNormal, () => {
let consoleMock1: jest.SpyInstance
beforeAll(() => {
consoleMock1 = jest.spyOn(console, "error").mockImplementation(() => void 0)
})
afterAll(() => {
consoleMock1.mockRestore()
})
test("should not write to disk", async () => {
Scaffold({
name: "app_name",
output: "output",
templates: ["input"],
data: { value: "1" },
verbose: 0,
dryRun: true,
})
expect(() => readFileSync(join(process.cwd(), "output", "app_name.txt"))).toThrow()
})
})
)
describe(
"outputPath override",
withMock(fileStructNormal, () => {
@@ -200,7 +227,7 @@ describe("Scaffold", () => {
verbose: 0,
})
const data = readFileSync(join(process.cwd(), "/custom-output/app_name/app_name.txt"))
expect(data.toString()).toBe("Hello, my app is app_name")
expect(data.toString()).toEqual("Hello, my app is app_name")
})
})
)
@@ -344,7 +371,7 @@ describe("Scaffold", () => {
})
const data = readFileSync(join(process.cwd(), "output", "app_name", "app_name.txt"))
expect(data.toString()).toBe("Hello, my app is app_name")
expect(data.toString()).toEqual("Hello, my app is app_name")
})
test("should work with default helper", async () => {
@@ -358,7 +385,7 @@ describe("Scaffold", () => {
})
const data = readFileSync(join(process.cwd(), "output", "APP_NAME", "app_name.txt"))
expect(data.toString()).toBe("Hello, my app is app_name")
expect(data.toString()).toEqual("Hello, my app is app_name")
})
test("should work with custom helper", async () => {
@@ -375,7 +402,7 @@ describe("Scaffold", () => {
})
const data = readFileSync(join(process.cwd(), "output", "REPLACED", "app_name.txt"))
expect(data.toString()).toBe("Hello, my app is app_name")
expect(data.toString()).toEqual("Hello, my app is app_name")
})
})
)
@@ -394,7 +421,7 @@ describe("Scaffold", () => {
})
const data = readFileSync(join(process.cwd(), "output", "app_name.txt"))
expect(data.toString()).toBe("Hello, my app is app_name")
expect(data.toString()).toEqual("Hello, my app is app_name")
})
test("should work with custom callback", async () => {
@@ -411,7 +438,7 @@ describe("Scaffold", () => {
})
const data = readFileSync(join(process.cwd(), "output", "app_name.txt"))
expect(data.toString()).toBe(
expect(data.toString()).toEqual(
[
"Hello, my app is app_name".toUpperCase(),
fileStructNormal.input["{{name}}.txt"],
@@ -419,6 +446,21 @@ describe("Scaffold", () => {
].join(", ")
)
})
test("should work with undefined response custom callback", async () => {
await Scaffold({
name: "app_name",
output: "output",
templates: ["input"],
verbose: 0,
data: {
value: "value",
},
beforeWrite: () => undefined,
})
const data = readFileSync(join(process.cwd(), "output", "app_name.txt"))
expect(data.toString()).toEqual("Hello, my app is app_name")
})
})
)
})