|
1 | 1 | import ncp from "ncp"; |
2 | 2 | import * as path from "path"; |
3 | 3 | import { promisify } from "util"; |
| 4 | +import { getArg } from "./utils/args"; |
4 | 5 |
|
5 | 6 | const copy = promisify(ncp); |
6 | 7 |
|
7 | | -const copyFiles = async (filePath: string): Promise<void> => { |
| 8 | +type CreateArgs = { |
| 9 | + dir: string; |
| 10 | + lang: string; |
| 11 | + testRunner: string; |
| 12 | +}; |
| 13 | + |
| 14 | +async function create(args: string[]): Promise<void> { |
| 15 | + let options: CreateArgs; |
| 16 | + |
| 17 | + // default . |
| 18 | + const dir = !args.length || args[0].match(/^-/) ? "." : args[0]; |
| 19 | + const lang = getArg(args, { name: "lang", alias: "l" }) || "js"; |
| 20 | + const testRunner = |
| 21 | + getArg(args, { name: "testRunner", alias: "t" }) || "mocha"; |
| 22 | + |
| 23 | + // validate lang |
| 24 | + if (!["js"].includes(lang)) { |
| 25 | + throw new Error(`Language ${lang} not supported yet in create`); |
| 26 | + } |
| 27 | + |
| 28 | + // validate test runner |
| 29 | + if (!["mocha"].includes(testRunner)) { |
| 30 | + throw new Error(`Test Runner ${testRunner} not supported yet in create`); |
| 31 | + } |
| 32 | + |
| 33 | + console.info(`Creating CodeRoad project for ${lang} ${testRunner}`); |
| 34 | + |
| 35 | + options = { |
| 36 | + dir, |
| 37 | + lang, |
| 38 | + testRunner, |
| 39 | + }; |
| 40 | + |
| 41 | + const localPath = path.join(process.cwd(), options.dir); |
| 42 | + |
| 43 | + // TODO: git init ? |
| 44 | + |
| 45 | + // copy tutorial file |
| 46 | + const pathToSrc = path.join(__dirname, "..", "src"); |
| 47 | + const templateDirectory = path.resolve(pathToSrc, "templates"); |
| 48 | + |
| 49 | + const markdownPath = path.join(templateDirectory, "TUTORIAL.md"); |
| 50 | + const targetMarkdownPath = path.join(localPath, "TUTORIAL.md"); |
8 | 51 | try { |
9 | | - const pathToSrc = path.join(__dirname, "..", "src"); |
10 | | - const templateDirectory = path.resolve(pathToSrc, "templates"); |
11 | | - const targetDirectory = process.cwd(); |
| 52 | + await copy(markdownPath, targetMarkdownPath, { |
| 53 | + clobber: false, |
| 54 | + }); |
| 55 | + } catch (e) { |
| 56 | + console.error("Error on creating markdown file"); |
| 57 | + console.error(e.message); |
| 58 | + } |
12 | 59 |
|
13 | | - await copy(templateDirectory, targetDirectory, { |
| 60 | + // TODO: copy master yaml |
| 61 | + const pathToYaml = path.join(templateDirectory, `${lang}-${testRunner}`); |
| 62 | + const targetYamlPath = path.join(localPath, "coderoad.yaml"); |
| 63 | + try { |
| 64 | + await copy(pathToYaml, targetYamlPath, { |
14 | 65 | clobber: false, |
15 | 66 | }); |
16 | 67 | } catch (e) { |
17 | | - console.log("Error on creating the files:"); |
18 | | - console.log(JSON.stringify(e, null, 1)); |
| 68 | + console.error("Error on creating yaml file"); |
| 69 | + console.error(e.message); |
19 | 70 | } |
20 | | -}; |
21 | 71 |
|
22 | | -const create = copyFiles; |
| 72 | + // TODO: copy code files with commits |
| 73 | +} |
23 | 74 |
|
24 | 75 | export default create; |
0 commit comments