All files / src/init index.ts

0% Statements 0/19
0% Branches 0/1
0% Functions 0/8
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import axios from "axios";
import * as tar from "tar";
 
import { Plugin } from "../cli";
 
export const DEFAULT_EXAMPLE = "yml";
 
export const EXAMPLES_ORG_NAME = "featurevisor";
export const EXAMPLES_REPO_NAME = "featurevisor";
export const EXAMPLES_BRANCH_NAME = "main";
 
export const EXAMPLES_TAR_URL = `https://codeload.github.com/${EXAMPLES_ORG_NAME}/${EXAMPLES_REPO_NAME}/tar.gz/${EXAMPLES_BRANCH_NAME}`;
 
function getExamplePath(exampleName: string) {
  return `${EXAMPLES_REPO_NAME}-${EXAMPLES_BRANCH_NAME}/examples/example-${exampleName}/`;
}
 
export function initProject(
  directoryPath: string,
  exampleName: string = DEFAULT_EXAMPLE,
): Promise<boolean> {
  return new Promise(function (resolve) {
    axios.get(EXAMPLES_TAR_URL, { responseType: "stream" }).then((response) => {
      response.data
        .pipe(
          tar.x({
            C: directoryPath,
            filter: (path) => path.indexOf(getExamplePath(exampleName)) === 0,
            strip: 3,
          }),
        )
        .on("error", (e) => {
          console.error(e);
 
          resolve(false);
        })
        .on("finish", () => {
          console.log(`Project scaffolded in ${directoryPath}`);
          console.log(``);
          console.log(`Please run "npm install" in the directory above.`);
 
          resolve(true);
        });
    });
  });
}
 
export const initPlugin: Plugin = {
  command: "init",
  handler: async function (options) {
    const { rootDirectoryPath, parsed } = options;
 
    await initProject(rootDirectoryPath, parsed.example);
  },
  examples: [
    {
      command: "init",
      description: "scaffold a new project in current directory",
    },
    {
      command: "init --example=exampleName",
      description: "scaffold a new project in current directory from known example",
    },
  ],
};