Skip to content

Commit

Permalink
✨ Support before, after scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jun 14, 2020
1 parent 407e5cf commit b270b7d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import got from "got";
import inquirer from "inquirer";
import ora from "ora";
import { join } from "path";
import { exception } from "console";

/** Carpent question (inquirer) type */
export interface CarpentQuestion {
Expand Down Expand Up @@ -64,15 +65,21 @@ export const carpent = async (
const spinner = ora("Cloning git repository").start();
const slug = slugify(defaultAnswers.dir);
await exec(`git clone ${defaultAnswers.repo} ${slug}`);
spinner.stop();
const path = join(".", slug);

// Find .carpentrc
let config: CarpentConfig = DEFAULT;
if (await pathExists(join(path, ".carpentrc")))
config = await readJson(join(path, ".carpentrc"));

// Run pre scripts
spinner.text = "Running scripts";
for await (const script of config.beforeAll ?? []) {
await exec(script);
}

// Ask questions
spinner.stop();
const allQuestions: CarpentQuestion[] = [
...(config.questions ?? []),
{
Expand All @@ -94,9 +101,15 @@ export const carpent = async (
? defaultAnswers
: await inquirer.prompt(allQuestions);

// Run pre scripts
spinner.text = "Running scripts";
spinner.start();
for await (const script of config.beforeAll ?? []) {
await exec(script);
}

// Update values
spinner.text = "Updating values";
spinner.start();
for await (const question of allQuestions) {
const VAL = question.replace
? (question.replace as string).replace(
Expand Down Expand Up @@ -162,8 +175,8 @@ export const carpent = async (

// Run post scripts
spinner.text = "Running scripts";
for await (const file of config.deleteFiles ?? []) {
await remove(join(slug, file));
for await (const script of config.afterAll ?? []) {
await exec(script);
}

// Return the result
Expand Down

0 comments on commit b270b7d

Please sign in to comment.