task-script-support-cli 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/package.json +1 -1
  2. package/dist/src/commands/configure.d.ts +2 -1
  3. package/dist/src/commands/configure.d.ts.map +1 -1
  4. package/dist/src/commands/configure.js +2 -1
  5. package/dist/src/commands/configure.js.map +1 -1
  6. package/dist/src/commands/gen.d.ts +2 -1
  7. package/dist/src/commands/gen.d.ts.map +1 -1
  8. package/dist/src/commands/gen.js +2 -0
  9. package/dist/src/commands/gen.js.map +1 -1
  10. package/dist/src/services/templater-service.d.ts +24 -0
  11. package/dist/src/services/templater-service.d.ts.map +1 -0
  12. package/dist/src/services/templater-service.js +134 -0
  13. package/dist/src/services/templater-service.js.map +1 -0
  14. package/dist/src/tasks/check-env.d.ts +0 -17
  15. package/dist/src/tasks/check-env.d.ts.map +1 -1
  16. package/dist/src/tasks/check-env.js +0 -53
  17. package/dist/src/tasks/check-env.js.map +1 -1
  18. package/dist/src/tasks/create-new-project.d.ts +12 -3
  19. package/dist/src/tasks/create-new-project.d.ts.map +1 -1
  20. package/dist/src/tasks/create-new-project.js +25 -15
  21. package/dist/src/tasks/create-new-project.js.map +1 -1
  22. package/dist/src/tasks/generate/project-context-guard.d.ts +42 -0
  23. package/dist/src/tasks/generate/project-context-guard.d.ts.map +1 -0
  24. package/dist/src/tasks/generate/project-context-guard.js +119 -0
  25. package/dist/src/tasks/generate/project-context-guard.js.map +1 -0
  26. package/dist/src/tasks/stdout/print-generated-results.d.ts +3 -1
  27. package/dist/src/tasks/stdout/print-generated-results.d.ts.map +1 -1
  28. package/dist/src/tasks/stdout/print-generated-results.js +14 -5
  29. package/dist/src/tasks/stdout/print-generated-results.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/commands/configure.ts +2 -1
  32. package/src/commands/gen.ts +2 -0
  33. package/src/services/templater-service.ts +136 -0
  34. package/src/tasks/check-env.ts +0 -55
  35. package/src/tasks/create-new-project.ts +28 -16
  36. package/src/tasks/generate/project-context-guard.ts +116 -0
  37. package/src/tasks/stdout/print-generated-results.ts +21 -4
  38. package/assets/yargs-template/task-runner/templater.sh +0 -69
  39. package/dist/assets/yargs-template/task-runner/templater.sh +0 -69
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const chalk_1 = __importDefault(require("chalk"));
16
+ const file_service_1 = require("../../services/file-service");
17
+ const app_task_1 = require("../../wrappers/app-task");
18
+ const tsyringe_1 = require("tsyringe");
19
+ /**
20
+ * Checks so see if we are in the context of a task-script-support
21
+ * project or not and fails the process with a friendly error message.
22
+ */
23
+ let ProjectContextGuard = class ProjectContextGuard extends app_task_1.AppTask {
24
+ fileService;
25
+ loggerName = "Project Context Check";
26
+ constructor(fileService) {
27
+ super();
28
+ this.fileService = fileService;
29
+ }
30
+ async run() {
31
+ this.logger.info(chalk_1.default.blueBright("Running Project Check"));
32
+ const errors = [];
33
+ try {
34
+ const projectDir = this.fileService.getRunnerRootDir();
35
+ this.checkProjectDependencies(projectDir, errors);
36
+ this.checkProjectDirectories(projectDir, errors);
37
+ }
38
+ catch (err) {
39
+ errors.push(`Unexpected error checking project: ${err}`);
40
+ }
41
+ if (errors.length) {
42
+ this.logger.error(`\n\n🚧 ${chalk_1.default.yellowBright("Note")}: This command requires execution from a ${chalk_1.default.blueBright("task-script-support")} project (folder)` +
43
+ `\n - Try running the ${chalk_1.default.blueBright("tssc " + chalk_1.default.bold("new"))} command to create a new project \n`, "Error Note");
44
+ }
45
+ this.checkToExit(errors);
46
+ this.setData({ environmentValidated: true });
47
+ }
48
+ /**
49
+ * Check for the folders we need to generate files in. Add errors
50
+ * when no target directories can be found. Saves project data to
51
+ * state so we don't have to refetch it later.
52
+ *
53
+ * @param projectDir the target project root directory path
54
+ * @param errors string array error messages are added to
55
+ */
56
+ checkProjectDirectories(projectDir, errors) {
57
+ const directories = {
58
+ taskFolders: this.fileService.getTaskDirs(projectDir),
59
+ serviceFolders: this.fileService.getServiceDirs(projectDir),
60
+ commandFolders: this.fileService.getCommandDirs(projectDir),
61
+ };
62
+ Object.keys(directories).forEach((k) => {
63
+ const key = k;
64
+ if (!directories[key] || !directories[key].length) {
65
+ errors.push(`Unable to find any ${k} in project`);
66
+ }
67
+ });
68
+ this.setData({
69
+ project: {
70
+ rootDir: projectDir,
71
+ ...directories,
72
+ },
73
+ });
74
+ }
75
+ /**
76
+ * Check that the project has the right dependencies installed. Adds
77
+ * to the errors array when they can't be found.
78
+ *
79
+ * @param projectDir the target project root directory path
80
+ * @param errors string array error messages are added to
81
+ */
82
+ checkProjectDependencies(projectDir, errors) {
83
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
84
+ const packagePath = require(this.fileService.join(projectDir, "package.json"));
85
+ const requireDeps = ["task-script-support", "tsyringe"];
86
+ requireDeps.forEach((d) => {
87
+ if (!Object.keys(packagePath.dependencies || {}).includes(d)) {
88
+ errors.push(`Missing required dependencies in project: '${d}'`);
89
+ }
90
+ });
91
+ }
92
+ /**
93
+ * Log and exit program if errors are present.
94
+ *
95
+ * @param errors the error messages array to check
96
+ */
97
+ checkToExit(errors) {
98
+ if (errors.length) {
99
+ this.logEnvErrors(errors);
100
+ process.exit(1);
101
+ }
102
+ }
103
+ /**
104
+ * Logs any error messages in the provided array.
105
+ *
106
+ * @param errors the error messages array to log
107
+ */
108
+ logEnvErrors(errors) {
109
+ if (errors.length) {
110
+ this.logger.error(new Error("Misconfigured Environment"), `Errors: \n${chalk_1.default.red(errors.join("\n"))}`);
111
+ }
112
+ }
113
+ };
114
+ ProjectContextGuard = __decorate([
115
+ (0, tsyringe_1.autoInjectable)(),
116
+ __metadata("design:paramtypes", [file_service_1.FileService])
117
+ ], ProjectContextGuard);
118
+ exports.default = ProjectContextGuard;
119
+ //# sourceMappingURL=project-context-guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project-context-guard.js","sourceRoot":"./src/","sources":["src/tasks/generate/project-context-guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,kDAA0B;AAC1B,8DAA0D;AAC1D,sDAAkD;AAClD,uCAA0C;AAE1C;;;GAGG;AAEY,IAAM,mBAAmB,GAAzB,MAAM,mBAAoB,SAAQ,kBAAO;IAGlC;IAFpB,UAAU,GAAG,uBAAuB,CAAC;IAErC,YAAoB,WAAwB;QAC1C,KAAK,EAAE,CAAC;QADU,gBAAW,GAAX,WAAW,CAAa;IAE5C,CAAC;IAED,KAAK,CAAC,GAAG;QACP,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,eAAK,CAAC,UAAU,CAAC,uBAAuB,CAAC,CAAC,CAAC;QAE5D,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,IAAI,CAAC;YACH,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,gBAAgB,EAAE,CAAC;YACvD,IAAI,CAAC,wBAAwB,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;YAClD,IAAI,CAAC,uBAAuB,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;QACnD,CAAC;QAAC,OAAO,GAAG,EAAE,CAA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WAAW;aACf;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACH,wBAAwB,CAAC,UAAkB,EAAE,MAAgB;QAC3D,iEAAiE;QACjE,MAAM,WAAW,GAAG,OAAO,CACzB,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,EAAE,cAAc,CAAC,CAClD,CAAC;QACF,MAAM,WAAW,GAAG,CAAC,qBAAqB,EAAE,UAAU,CAAC,CAAC;QACxD,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;YACxB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC7D,MAAM,CAAC,IAAI,CAAC,8CAA8C,CAAC,GAAG,CAAC,CAAC;YAClE,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACK,WAAW,CAAC,MAAgB;QAClC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YAClB,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;YAC1B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;IACH,CAAC;IAED;;;;OAIG;IACK,YAAY,CAAC,MAAgB;QACnC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YAClB,IAAI,CAAC,MAAM,CAAC,KAAK,CACf,IAAI,KAAK,CAAC,2BAA2B,CAAC,EACtC,aAAa,eAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAC5C,CAAC;QACJ,CAAC;IACH,CAAC;CACF,CAAA;AAxGoB,mBAAmB;IADvC,IAAA,yBAAc,GAAE;qCAIkB,0BAAW;GAHzB,mBAAmB,CAwGvC;kBAxGoB,mBAAmB"}
@@ -1,12 +1,14 @@
1
1
  import { AppTask } from "../../wrappers/app-task";
2
2
  import { UtilService } from "../../services/util-service";
3
+ import { FileService } from "../../services/file-service";
3
4
  /**
4
5
  * Prints the generated output results
5
6
  */
6
7
  export default class PrintGeneratedResults extends AppTask {
7
8
  private utilService;
9
+ private fileService;
8
10
  loggerName: string;
9
- constructor(utilService: UtilService);
11
+ constructor(utilService: UtilService, fileService: FileService);
10
12
  run(): Promise<void>;
11
13
  }
12
14
  //# sourceMappingURL=print-generated-results.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"print-generated-results.d.ts","sourceRoot":"./src/","sources":["src/tasks/stdout/print-generated-results.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAG1D;;GAEG;AAEH,MAAM,CAAC,OAAO,OAAO,qBAAsB,SAAQ,OAAO;IAG5C,OAAO,CAAC,WAAW;IAF/B,UAAU,SAAmB;gBAET,WAAW,EAAE,WAAW;IAItC,GAAG;CAmBV"}
1
+ {"version":3,"file":"print-generated-results.d.ts","sourceRoot":"./src/","sources":["src/tasks/stdout/print-generated-results.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAO1D;;GAEG;AAEH,MAAM,CAAC,OAAO,OAAO,qBAAsB,SAAQ,OAAO;IAItD,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,WAAW;IAJrB,UAAU,SAAmB;gBAGnB,WAAW,EAAE,WAAW,EACxB,WAAW,EAAE,WAAW;IAK5B,GAAG;CA4BV"}
@@ -16,16 +16,22 @@ const chalk_1 = __importDefault(require("chalk"));
16
16
  const app_task_1 = require("../../wrappers/app-task");
17
17
  const tsyringe_1 = require("tsyringe");
18
18
  const util_service_1 = require("../../services/util-service");
19
- const project_service_1 = require("../../services/project-service");
19
+ const file_service_1 = require("../../services/file-service");
20
+ const newProjectMessage = (dest) => `
21
+ Try:
22
+ cd ${dest} && npm i && npm start -- help
23
+ `;
20
24
  /**
21
25
  * Prints the generated output results
22
26
  */
23
27
  let PrintGeneratedResults = class PrintGeneratedResults extends app_task_1.AppTask {
24
28
  utilService;
29
+ fileService;
25
30
  loggerName = "Print Results";
26
- constructor(utilService) {
31
+ constructor(utilService, fileService) {
27
32
  super();
28
33
  this.utilService = utilService;
34
+ this.fileService = fileService;
29
35
  }
30
36
  async run() {
31
37
  const genType = this.state.data.genTargetType
@@ -37,16 +43,19 @@ let PrintGeneratedResults = class PrintGeneratedResults extends app_task_1.AppTa
37
43
  await this.utilService.sleep(200);
38
44
  console.log(`
39
45
 
40
- Generated new ${this.state.data.outputDestination?.includes(project_service_1.ProjectService.defaults.extention) ? "file" : "project"}:
46
+ Generated new ${genType !== "Project" ? "file" : "project"}:
41
47
 
42
48
  ${this.state.data.outputDestination || "<None>"}
43
-
49
+ ${genType === "Project"
50
+ ? `\n${newProjectMessage(this.fileService.toRelativePath(this.fileService.getRunnerDir(), this.state.data.outputDestination))}\n`
51
+ : ""}
44
52
  `);
45
53
  }
46
54
  };
47
55
  PrintGeneratedResults = __decorate([
48
56
  (0, tsyringe_1.autoInjectable)(),
49
- __metadata("design:paramtypes", [util_service_1.UtilService])
57
+ __metadata("design:paramtypes", [util_service_1.UtilService,
58
+ file_service_1.FileService])
50
59
  ], PrintGeneratedResults);
51
60
  exports.default = PrintGeneratedResults;
52
61
  //# sourceMappingURL=print-generated-results.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"print-generated-results.js","sourceRoot":"./src/","sources":["src/tasks/stdout/print-generated-results.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,kDAA0B;AAC1B,sDAAkD;AAClD,uCAA0C;AAC1C,8DAA0D;AAC1D,oEAAgE;AAEhE;;GAEG;AAEY,IAAM,qBAAqB,GAA3B,MAAM,qBAAsB,SAAQ,kBAAO;IAGpC;IAFpB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AAqB,CAsCzC;kBAtCoB,qBAAqB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "task-script-support-cli",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "main": "index.js",
5
5
  "type": "commonjs",
6
6
  "preferGlobal": true,
@@ -3,8 +3,9 @@ import { Command } from "../wrappers/command";
3
3
  import CheckEnv from "../tasks/check-env";
4
4
  import ConfigureCache from "../tasks/configure";
5
5
  import PrintBanner from "../tasks/stdout/print-banner";
6
+ import ProjectContextGuard from "../tasks/generate/project-context-guard";
6
7
 
7
8
  @singleton()
8
9
  export class ConfigureCommand extends Command {
9
- tasks = [PrintBanner, CheckEnv, ConfigureCache];
10
+ tasks = [PrintBanner, CheckEnv, ProjectContextGuard, ConfigureCache];
10
11
  }
@@ -9,12 +9,14 @@ import GenerateTask from "../tasks/generate/generate-task";
9
9
  import PrintGeneratedResults from "../tasks/stdout/print-generated-results";
10
10
  import SyncConfiguration from "../tasks/sync-configuration";
11
11
  import SelectGenTargetName from "../tasks/generate/select-gen-target-name";
12
+ import ProjectContextGuard from "../tasks/generate/project-context-guard";
12
13
 
13
14
  @singleton()
14
15
  export class GenCommand extends Command {
15
16
  tasks = [
16
17
  PrintBanner,
17
18
  CheckEnvironment,
19
+ ProjectContextGuard,
18
20
  SelectGenTarget,
19
21
  SyncConfiguration,
20
22
  SelectGenTargetName,
@@ -0,0 +1,136 @@
1
+ import path from "node:path";
2
+ import fs from "node:fs";
3
+ import { autoInjectable } from "tsyringe";
4
+ import { SpawnService } from "./spawn-service";
5
+ import { LogService } from "./log-service";
6
+
7
+ const gitignoreContent = `node_modules
8
+ dist
9
+ .env`;
10
+
11
+ @autoInjectable()
12
+ export class TemplateService {
13
+ private loggerName = "Template Service";
14
+
15
+ private outputName!: string;
16
+ private sourceDir!: string;
17
+ private destination!: string;
18
+
19
+ private dirListToCopy: string[] = [".vscode", "src", "tests"];
20
+ private fileListToCopy: string[] = [
21
+ ".prettierignore",
22
+ ".prettierrc",
23
+ "task-runner",
24
+ "install-link.sh",
25
+ "eslint.config.ts",
26
+ "vitest.config.ts",
27
+ "package-lock.json",
28
+ "package.json",
29
+ "tsconfig.json",
30
+ ];
31
+ private nameRefList: string[] = [
32
+ // files that have name references
33
+ "package.json",
34
+ "package-lock.json",
35
+ "install-link.sh",
36
+ ];
37
+
38
+ constructor(
39
+ private spawnService: SpawnService,
40
+ private logger: LogService,
41
+ ) {
42
+ this.sourceDir = path.join(
43
+ __dirname,
44
+ "../../assets/yargs-template/task-runner/",
45
+ );
46
+ this.logger.setPrefix(this.loggerName);
47
+ }
48
+
49
+ public async runTemplater(outputName: string) {
50
+ if (!outputName) {
51
+ throw new Error(
52
+ "Missing required parameter. Usage: Templater <output-name>",
53
+ );
54
+ }
55
+ this.outputName = outputName;
56
+ this.destination = path.join(process.cwd(), outputName);
57
+
58
+ await this.ensureDestinationExists();
59
+ await this.copyDirectories();
60
+ await this.copyFiles();
61
+ await this.fixNaming();
62
+ await this.createGitignore();
63
+ await this.removeDoubleNestedTestsDirectory();
64
+ await this.renameTaskRunner();
65
+ await this.initializeWithGit();
66
+ await this.logger.flush();
67
+ }
68
+
69
+ private async ensureDestinationExists() {
70
+ if (!fs.existsSync(this.destination)) {
71
+ fs.mkdirSync(this.destination, { recursive: true });
72
+ }
73
+ }
74
+
75
+ private async copyDirectories() {
76
+ for (const folder of this.dirListToCopy) {
77
+ const src = path.join(this.sourceDir, folder);
78
+ const dest = path.join(this.destination, folder);
79
+ this.logger.debug(`Running Copy: cp -R ${src} ${dest}`);
80
+ fs.cpSync(src, dest, { recursive: true });
81
+ }
82
+ }
83
+
84
+ private async copyFiles() {
85
+ for (const filename of this.fileListToCopy) {
86
+ const src = path.join(this.sourceDir, filename);
87
+ const dest = path.join(this.destination, filename);
88
+ this.logger.debug(`Running Copy: cp ${src} ${dest}`);
89
+ fs.copyFileSync(src, dest);
90
+ }
91
+ }
92
+
93
+ private async fixNaming() {
94
+ for (const fileToEdit of this.nameRefList) {
95
+ const dest = path.join(this.destination, fileToEdit);
96
+ const replacePattern = new RegExp("task-runner", "g");
97
+ const content = fs
98
+ .readFileSync(dest, "utf-8")
99
+ .replaceAll(replacePattern, this.outputName);
100
+ fs.writeFileSync(dest, content);
101
+ }
102
+ }
103
+
104
+ private async createGitignore() {
105
+ fs.writeFileSync(
106
+ path.join(this.destination, ".gitignore"),
107
+ gitignoreContent,
108
+ );
109
+ }
110
+
111
+ private async removeDoubleNestedTestsDirectory() {
112
+ const doubleNestedTestsPath = path.join(this.destination, "tests", "tests");
113
+ if (fs.existsSync(doubleNestedTestsPath)) {
114
+ this.logger.debug(
115
+ `Removing double nested tests directory: ${doubleNestedTestsPath}`,
116
+ );
117
+ fs.rmSync(doubleNestedTestsPath, { recursive: true });
118
+ }
119
+ }
120
+
121
+ private async renameTaskRunner() {
122
+ const src = path.join(this.destination, "task-runner");
123
+ const dest = path.join(this.destination, this.outputName);
124
+ this.logger.debug(`Renaming task-runner to ${this.outputName}`);
125
+ fs.renameSync(src, dest);
126
+ }
127
+
128
+ private async initializeWithGit() {
129
+ try {
130
+ this.logger.debug("Initializing with git");
131
+ this.spawnService.execSyncFromDir(`git init -b main`, this.destination);
132
+ } catch (error) {
133
+ console.error("Error initializing git:", (error as Error).message);
134
+ }
135
+ }
136
+ }
@@ -3,7 +3,6 @@ import { AppTask } from "../wrappers/app-task";
3
3
  import { autoInjectable } from "tsyringe";
4
4
  import { EnvironmentConfigKeys } from "../types/state";
5
5
  import { FileService } from "../services/file-service";
6
- import path from "path";
7
6
 
8
7
  /**
9
8
  * Checks the environment configuration
@@ -35,64 +34,10 @@ export default class CheckEnvironment extends AppTask {
35
34
  const errors: string[] = this.getErrorMessages(this.requiredEnvVars);
36
35
  this.checkToExit(errors);
37
36
 
38
- try {
39
- const projectDir = this.fileService.getRunnerRootDir();
40
- this.checkProjectDependencies(projectDir, errors);
41
- this.checkProjectDirectories(projectDir, errors);
42
- } catch (err) {
43
- errors.push(`Unexpected error checking project: ${err}`);
44
- }
45
-
46
37
  this.checkToExit(errors);
47
38
  this.setData({ environmentValidated: true });
48
39
  }
49
40
 
50
- /**
51
- * Check for the folders we need to generate files in. Add errors
52
- * when no target directories can be found. Saves project data to
53
- * state so we don't have to refetch it later.
54
- *
55
- * @param projectDir the target project root directory path
56
- * @param errors string array error messages are added to
57
- */
58
- checkProjectDirectories(projectDir: string, errors: string[]) {
59
- const directories = {
60
- taskFolders: this.fileService.getTaskDirs(projectDir),
61
- serviceFolders: this.fileService.getServiceDirs(projectDir),
62
- commandFolders: this.fileService.getCommandDirs(projectDir),
63
- };
64
- Object.keys(directories).forEach((k: string) => {
65
- const key: keyof typeof directories = k as keyof typeof directories;
66
- if (!directories[key] || !directories[key].length) {
67
- errors.push(`Unable to find any ${k} in project`);
68
- }
69
- });
70
- this.setData({
71
- project: {
72
- rootDir: projectDir,
73
- ...directories,
74
- },
75
- });
76
- }
77
-
78
- /**
79
- * Check that the project has the right dependencies installed. Adds
80
- * to the errors array when they can't be found.
81
- *
82
- * @param projectDir the target project root directory path
83
- * @param errors string array error messages are added to
84
- */
85
- checkProjectDependencies(projectDir: string, errors: string[]) {
86
- // eslint-disable-next-line @typescript-eslint/no-require-imports
87
- const packagePath = require(path.join(projectDir, "package.json"));
88
- const requireDeps = ["task-script-support", "tsyringe"];
89
- requireDeps.forEach((d) => {
90
- if (!Object.keys(packagePath.dependencies || {}).includes(d)) {
91
- errors.push(`Missing required dependencies in project: '${d}'`);
92
- }
93
- });
94
- }
95
-
96
41
  /**
97
42
  * Log and exit program if errors are present.
98
43
  *
@@ -1,9 +1,11 @@
1
- import { SpawnService } from "../services/spawn-service";
2
1
  import { FileService } from "../services/file-service";
3
2
  import { PromptService } from "../services/prompt-service";
4
3
  import { CLIOptions } from "../types/process";
5
4
  import { AppTask } from "../wrappers/app-task";
6
5
  import { autoInjectable } from "tsyringe";
6
+ import { TemplateService } from "../services/templater-service";
7
+ import { UtilService } from "../services/util-service";
8
+ import { CaseType } from "../types/format";
7
9
 
8
10
  /**
9
11
  * Creates a New Task Script Support Project
@@ -14,8 +16,9 @@ export default class CreateNewProject extends AppTask {
14
16
 
15
17
  constructor(
16
18
  private fileService: FileService,
17
- private spawnService: SpawnService,
19
+ private utilService: UtilService,
18
20
  private promptService: PromptService,
21
+ private templaterService: TemplateService,
19
22
  ) {
20
23
  super();
21
24
  }
@@ -25,24 +28,17 @@ export default class CreateNewProject extends AppTask {
25
28
  * newly generated project path.
26
29
  */
27
30
  async run() {
28
- // get path of the templater
29
- const templaterFullPath = this.fileService.join(
30
- __dirname,
31
- "../../assets/yargs-template/task-runner/templater.sh",
32
- );
33
-
34
- // get execution directory
35
- const runnerDir = this.fileService.getRunnerDir();
36
-
37
31
  // get project name user input
38
32
  const projectName: string = await this.getProjectName();
39
33
 
40
- // execute the templater from the runner directory and provide given name
41
- const shCmd = `bash ${templaterFullPath} ${projectName}`;
42
- this.spawnService.execSyncFromDir(shCmd, runnerDir);
34
+ // run the templater
35
+ await this.templaterService.runTemplater(projectName);
43
36
 
44
37
  this.setData({
45
- outputDestination: this.fileService.join(runnerDir, projectName),
38
+ outputDestination: this.fileService.join(
39
+ this.fileService.getRunnerDir(),
40
+ projectName,
41
+ ),
46
42
  });
47
43
  }
48
44
 
@@ -61,6 +57,22 @@ export default class CreateNewProject extends AppTask {
61
57
  if (!projectName) {
62
58
  throw new Error("Unable to resolve projectName");
63
59
  }
64
- return projectName;
60
+ return this.normalizeProjectName(projectName);
61
+ }
62
+
63
+ /**
64
+ * Normalize the name to the provided case or kebab.
65
+ *
66
+ * @param projectName the target project name to normalize
67
+ * @returns the normalized project name
68
+ */
69
+ normalizeProjectName(projectName: string) {
70
+ const normalizedTarget: CaseType =
71
+ this.utilService.detectCase(`${projectName}.ts`) || CaseType.KEBAB_CASE;
72
+
73
+ return this.utilService.titleizedToCase(
74
+ this.utilService.titleizeAll(projectName),
75
+ normalizedTarget,
76
+ );
65
77
  }
66
78
  }
@@ -0,0 +1,116 @@
1
+ import chalk from "chalk";
2
+ import { FileService } from "../../services/file-service";
3
+ import { AppTask } from "../../wrappers/app-task";
4
+ import { autoInjectable } from "tsyringe";
5
+
6
+ /**
7
+ * Checks so see if we are in the context of a task-script-support
8
+ * project or not and fails the process with a friendly error message.
9
+ */
10
+ @autoInjectable()
11
+ export default class ProjectContextGuard extends AppTask {
12
+ loggerName = "Project Context Check";
13
+
14
+ constructor(private fileService: FileService) {
15
+ super();
16
+ }
17
+
18
+ async run() {
19
+ this.logger.info(chalk.blueBright("Running Project Check"));
20
+
21
+ const errors: string[] = [];
22
+ try {
23
+ const projectDir = this.fileService.getRunnerRootDir();
24
+ this.checkProjectDependencies(projectDir, errors);
25
+ this.checkProjectDirectories(projectDir, errors);
26
+ } catch (err) {
27
+ errors.push(`Unexpected error checking project: ${err}`);
28
+ }
29
+
30
+ if (errors.length) {
31
+ // give jimmy a message
32
+ this.logger.error(
33
+ `\n\n🚧 ${chalk.yellowBright("Note")}: This command requires execution from a ${chalk.blueBright("task-script-support")} project (folder)` +
34
+ `\n - Try running the ${chalk.blueBright("tssc " + chalk.bold("new"))} command to create a new project \n`,
35
+ "Error Note",
36
+ );
37
+ }
38
+
39
+ this.checkToExit(errors);
40
+ this.setData({ environmentValidated: true });
41
+ }
42
+
43
+ /**
44
+ * Check for the folders we need to generate files in. Add errors
45
+ * when no target directories can be found. Saves project data to
46
+ * state so we don't have to refetch it later.
47
+ *
48
+ * @param projectDir the target project root directory path
49
+ * @param errors string array error messages are added to
50
+ */
51
+ checkProjectDirectories(projectDir: string, errors: string[]) {
52
+ const directories = {
53
+ taskFolders: this.fileService.getTaskDirs(projectDir),
54
+ serviceFolders: this.fileService.getServiceDirs(projectDir),
55
+ commandFolders: this.fileService.getCommandDirs(projectDir),
56
+ };
57
+ Object.keys(directories).forEach((k: string) => {
58
+ const key: keyof typeof directories = k as keyof typeof directories;
59
+ if (!directories[key] || !directories[key].length) {
60
+ errors.push(`Unable to find any ${k} in project`);
61
+ }
62
+ });
63
+ this.setData({
64
+ project: {
65
+ rootDir: projectDir,
66
+ ...directories,
67
+ },
68
+ });
69
+ }
70
+
71
+ /**
72
+ * Check that the project has the right dependencies installed. Adds
73
+ * to the errors array when they can't be found.
74
+ *
75
+ * @param projectDir the target project root directory path
76
+ * @param errors string array error messages are added to
77
+ */
78
+ checkProjectDependencies(projectDir: string, errors: string[]) {
79
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
80
+ const packagePath = require(
81
+ this.fileService.join(projectDir, "package.json"),
82
+ );
83
+ const requireDeps = ["task-script-support", "tsyringe"];
84
+ requireDeps.forEach((d) => {
85
+ if (!Object.keys(packagePath.dependencies || {}).includes(d)) {
86
+ errors.push(`Missing required dependencies in project: '${d}'`);
87
+ }
88
+ });
89
+ }
90
+
91
+ /**
92
+ * Log and exit program if errors are present.
93
+ *
94
+ * @param errors the error messages array to check
95
+ */
96
+ private checkToExit(errors: string[]) {
97
+ if (errors.length) {
98
+ this.logEnvErrors(errors);
99
+ process.exit(1);
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Logs any error messages in the provided array.
105
+ *
106
+ * @param errors the error messages array to log
107
+ */
108
+ private logEnvErrors(errors: string[]): void {
109
+ if (errors.length) {
110
+ this.logger.error(
111
+ new Error("Misconfigured Environment"),
112
+ `Errors: \n${chalk.red(errors.join("\n"))}`,
113
+ );
114
+ }
115
+ }
116
+ }