@dittowords/cli 2.5.1 → 2.6.0

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 (73) hide show
  1. package/.idea/workspace.xml +124 -0
  2. package/README.md +23 -10
  3. package/babel.config.js +2 -8
  4. package/bin/add-project.js +38 -0
  5. package/bin/add-project.js.map +1 -0
  6. package/bin/api.js +20 -0
  7. package/bin/api.js.map +1 -0
  8. package/bin/config.js +174 -0
  9. package/bin/config.js.map +1 -0
  10. package/bin/consts.js +21 -0
  11. package/bin/consts.js.map +1 -0
  12. package/bin/ditto.js +71 -74
  13. package/bin/ditto.js.map +1 -0
  14. package/bin/init/init.js +39 -0
  15. package/bin/init/init.js.map +1 -0
  16. package/bin/init/project.js +115 -0
  17. package/bin/init/project.js.map +1 -0
  18. package/bin/init/token.js +89 -0
  19. package/bin/init/token.js.map +1 -0
  20. package/bin/output.js +34 -0
  21. package/bin/output.js.map +1 -0
  22. package/bin/pull.js +305 -0
  23. package/bin/pull.js.map +1 -0
  24. package/bin/remove-project.js +40 -0
  25. package/bin/remove-project.js.map +1 -0
  26. package/bin/types.js +3 -0
  27. package/bin/types.js.map +1 -0
  28. package/bin/utils/getSelectedProjects.js +76 -0
  29. package/bin/utils/getSelectedProjects.js.map +1 -0
  30. package/bin/utils/processMetaOption.js +15 -0
  31. package/bin/utils/processMetaOption.js.map +1 -0
  32. package/bin/utils/projectsToText.js +16 -0
  33. package/bin/utils/projectsToText.js.map +1 -0
  34. package/bin/utils/promptForProject.js +44 -0
  35. package/bin/utils/promptForProject.js.map +1 -0
  36. package/bin/utils/sourcesToText.js +25 -0
  37. package/bin/utils/sourcesToText.js.map +1 -0
  38. package/jest.config.js +1 -1
  39. package/lib/{add-project.js → add-project.ts} +8 -8
  40. package/lib/api.ts +15 -0
  41. package/lib/{config.test.js → config.test.ts} +14 -25
  42. package/lib/config.ts +214 -0
  43. package/lib/consts.ts +20 -0
  44. package/lib/ditto.ts +97 -0
  45. package/lib/init/{init.js → init.ts} +11 -12
  46. package/lib/init/project.test.ts +49 -0
  47. package/lib/init/{project.js → project.ts} +31 -29
  48. package/lib/init/{token.test.js → token.test.ts} +3 -3
  49. package/lib/init/{token.js → token.ts} +23 -19
  50. package/lib/output.ts +21 -0
  51. package/lib/pull.test.ts +172 -0
  52. package/lib/{pull.js → pull.ts} +145 -98
  53. package/lib/{remove-project.js → remove-project.ts} +11 -15
  54. package/lib/types.ts +23 -0
  55. package/lib/utils/getSelectedProjects.ts +55 -0
  56. package/lib/utils/processMetaOption.test.ts +18 -0
  57. package/lib/utils/processMetaOption.ts +16 -0
  58. package/lib/utils/{projectsToText.js → projectsToText.ts} +5 -4
  59. package/lib/utils/{promptForProject.js → promptForProject.ts} +18 -9
  60. package/lib/utils/{sourcesToText.js → sourcesToText.ts} +6 -5
  61. package/package.json +20 -15
  62. package/testing/fixtures/project-config-pull.yml +0 -0
  63. package/tsconfig.json +12 -0
  64. package/yarn-error.log +4466 -0
  65. package/lib/api.js +0 -18
  66. package/lib/config.js +0 -169
  67. package/lib/consts.js +0 -14
  68. package/lib/init/project.test.js +0 -99
  69. package/lib/output.js +0 -21
  70. package/lib/pull.test.js +0 -173
  71. package/lib/utils/getSelectedProjects.js +0 -44
  72. package/lib/utils/processMetaOption.js +0 -16
  73. package/lib/utils/processMetaOption.test.js +0 -16
@@ -1,11 +1,11 @@
1
- const config = require("./config");
2
- const consts = require("./consts");
3
- const output = require("./output");
4
- const {
1
+ import config from "./config";
2
+ import consts from "./consts";
3
+ import output from "./output";
4
+ import {
5
5
  getSelectedProjects,
6
6
  getIsUsingComponents,
7
- } = require("./utils/getSelectedProjects");
8
- const promptForProject = require("./utils/promptForProject");
7
+ } from "./utils/getSelectedProjects";
8
+ import promptForProject from "./utils/promptForProject";
9
9
 
10
10
  async function removeProject() {
11
11
  const projects = getSelectedProjects();
@@ -13,17 +13,14 @@ async function removeProject() {
13
13
  if (!projects.length && !isUsingComponents) {
14
14
  console.log(
15
15
  "\n" +
16
- "No projects found in your workspace.\n" +
16
+ "No projects found in your current configuration.\n" +
17
17
  `Try adding one with: ${output.info("ditto-cli project add")}\n`
18
18
  );
19
19
  return;
20
20
  }
21
21
 
22
22
  const allProjects = isUsingComponents
23
- ? [
24
- { id: "ditto_component_library", name: "Ditto Component Library" },
25
- ...projects,
26
- ]
23
+ ? [{ id: "components", name: "Ditto Component Library" }, ...projects]
27
24
  : projects;
28
25
 
29
26
  const projectToRemove = await promptForProject({
@@ -34,9 +31,8 @@ async function removeProject() {
34
31
  });
35
32
  if (!projectToRemove) return;
36
33
 
37
- config.writeData(consts.PROJECT_CONFIG_FILE, {
38
- components:
39
- isUsingComponents && projectToRemove.id !== "ditto_component_library",
34
+ config.writeProjectConfigData(consts.PROJECT_CONFIG_FILE, {
35
+ components: isUsingComponents && projectToRemove.id !== "components",
40
36
  projects: projects.filter(({ id }) => id !== projectToRemove.id),
41
37
  });
42
38
 
@@ -51,4 +47,4 @@ async function removeProject() {
51
47
  );
52
48
  }
53
49
 
54
- module.exports = removeProject;
50
+ export default removeProject;
package/lib/types.ts ADDED
@@ -0,0 +1,23 @@
1
+ export interface Project {
2
+ name: string;
3
+ id: string;
4
+ url?: string;
5
+ fileName?: string;
6
+ }
7
+
8
+ export interface ConfigYAML {
9
+ components?: boolean;
10
+ projects?: Project[];
11
+ format?: string;
12
+ variants?: boolean;
13
+ }
14
+
15
+ export interface SourceInformation {
16
+ hasSourceData: boolean;
17
+ validProjects: Project[];
18
+ shouldFetchComponentLibrary: boolean;
19
+ variants: boolean;
20
+ format: string | undefined;
21
+ }
22
+
23
+ export type Token = string | undefined;
@@ -0,0 +1,55 @@
1
+ import fs from "fs";
2
+ import yaml, { YAMLException } from "js-yaml";
3
+
4
+ import { PROJECT_CONFIG_FILE } from "../consts";
5
+ import { ConfigYAML, Project } from "../types";
6
+
7
+ function jsonIsConfigYAML(json: unknown): json is ConfigYAML {
8
+ return typeof json === "object";
9
+ }
10
+
11
+ function yamlToJson(_yaml: string): ConfigYAML | null {
12
+ try {
13
+ let configYaml = yaml.load(_yaml);
14
+ if (!jsonIsConfigYAML(configYaml)) {
15
+ return {};
16
+ }
17
+ return configYaml;
18
+ } catch (e) {
19
+ if (e instanceof YAMLException) {
20
+ return null;
21
+ } else {
22
+ throw e;
23
+ }
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Returns an array containing all valid projects ({ id, name })
29
+ * currently contained in the project config file.
30
+ */
31
+ export const getSelectedProjects = (
32
+ configFile = PROJECT_CONFIG_FILE
33
+ ): Project[] => {
34
+ if (!fs.existsSync(configFile)) return [];
35
+
36
+ const contentYaml = fs.readFileSync(configFile, "utf8");
37
+ const contentJson = yamlToJson(contentYaml);
38
+
39
+ if (!(contentJson && contentJson.projects)) {
40
+ return [];
41
+ }
42
+
43
+ return contentJson.projects.filter(({ name, id }) => name && id);
44
+ };
45
+
46
+ export const getIsUsingComponents = (
47
+ configFile = PROJECT_CONFIG_FILE
48
+ ): boolean => {
49
+ if (!fs.existsSync(configFile)) return false;
50
+
51
+ const contentYaml = fs.readFileSync(configFile, "utf8");
52
+ const contentJson = yamlToJson(contentYaml);
53
+
54
+ return !!contentJson && !!contentJson.components;
55
+ };
@@ -0,0 +1,18 @@
1
+ import processMetaOption from "./processMetaOption";
2
+
3
+ describe("processMetaOption tests", () => {
4
+ it("It parses correctly", () => {
5
+ expect(
6
+ processMetaOption(["githubActionRequest:true", "trigger:manual"])
7
+ ).toEqual({
8
+ githubActionRequest: "true",
9
+ trigger: "manual",
10
+ });
11
+ });
12
+ it("Malformed doesnt crash", () => {
13
+ expect(processMetaOption(["context:github-action", "trigger"])).toEqual({
14
+ context: "github-action",
15
+ trigger: undefined,
16
+ });
17
+ });
18
+ });
@@ -0,0 +1,16 @@
1
+ const processMetaOption = (inputArr: string[] | null) => {
2
+ const res: Record<string, string> = {};
3
+
4
+ if (!Array.isArray(inputArr)) {
5
+ return res;
6
+ }
7
+
8
+ inputArr.forEach((element) => {
9
+ const [key, value] = element.split(":");
10
+ res[key] = value;
11
+ });
12
+
13
+ return res;
14
+ };
15
+
16
+ export default processMetaOption;
@@ -1,6 +1,7 @@
1
- const output = require("../output");
1
+ import output from "../output";
2
+ import { Project } from "../types";
2
3
 
3
- function projectsToText(projects) {
4
+ const projectsToText = (projects: Project[]) => {
4
5
  return (
5
6
  (projects || []).reduce(
6
7
  (outputString, { name, id }) =>
@@ -13,6 +14,6 @@ function projectsToText(projects) {
13
14
  ""
14
15
  ) + "\n"
15
16
  );
16
- }
17
+ };
17
18
 
18
- module.exports = projectsToText;
19
+ export default projectsToText;
@@ -1,18 +1,17 @@
1
1
  const { AutoComplete } = require("enquirer");
2
2
 
3
- const output = require("../output");
3
+ import output from "../output";
4
+ import { Project } from "../types";
4
5
 
5
- function formatProjectChoice(project) {
6
+ function formatProjectChoice(project: Project) {
6
7
  return (
7
8
  project.name +
8
9
  " " +
9
- output.subtle(
10
- project.url || `https://app.dittowords.com/doc/${project.id}`
11
- )
10
+ output.subtle(project.url || `https://app.dittowords.com/doc/${project.id}`)
12
11
  );
13
12
  }
14
13
 
15
- function parseResponse(response) {
14
+ function parseResponse(response: string) {
16
15
  if (!response) {
17
16
  return null;
18
17
  }
@@ -26,7 +25,17 @@ function parseResponse(response) {
26
25
  return { name, id };
27
26
  }
28
27
 
29
- async function promptForProject({ message, projects, limit = 10 }) {
28
+ interface ProjectPromptParams {
29
+ message: string;
30
+ projects: Project[];
31
+ limit?: number;
32
+ }
33
+
34
+ const promptForProject = async ({
35
+ message,
36
+ projects,
37
+ limit = 10,
38
+ }: ProjectPromptParams) => {
30
39
  output.nl();
31
40
 
32
41
  const choices = projects.map(formatProjectChoice);
@@ -48,6 +57,6 @@ async function promptForProject({ message, projects, limit = 10 }) {
48
57
  }
49
58
 
50
59
  return parseResponse(response);
51
- }
60
+ };
52
61
 
53
- module.exports = promptForProject;
62
+ export default promptForProject;
@@ -1,7 +1,8 @@
1
- const output = require("../output");
2
- const projectsToText = require("./projectsToText");
1
+ import output from "../output";
2
+ import projectsToText from "./projectsToText";
3
+ import { Project } from "../types";
3
4
 
4
- function sourcesToText(projects, componentLibrary) {
5
+ const sourcesToText = (projects: Project[], componentLibrary: boolean) => {
5
6
  let message = "";
6
7
 
7
8
  if (componentLibrary) {
@@ -19,6 +20,6 @@ function sourcesToText(projects, componentLibrary) {
19
20
  }
20
21
 
21
22
  return message;
22
- }
23
+ };
23
24
 
24
- module.exports = sourcesToText;
25
+ export default sourcesToText;
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@dittowords/cli",
3
- "version": "2.5.1",
3
+ "version": "2.6.0",
4
4
  "description": "Command Line Interface for Ditto (dittowords.com).",
5
5
  "main": "bin/index.js",
6
6
  "scripts": {
7
+ "prepublish": "tsc",
7
8
  "prepare": "husky install",
8
- "sync": "node bin/ditto.js pull"
9
+ "start": "tsc && node bin/ditto.js",
10
+ "sync": "tsc && node bin/ditto.js pull",
11
+ "dev": "tsc --watch"
9
12
  },
10
13
  "repository": {
11
14
  "type": "git",
@@ -25,36 +28,38 @@
25
28
  "cli",
26
29
  "api"
27
30
  ],
31
+ "types": "bin/ditto.d.ts",
28
32
  "bin": {
29
- "ditto-cli": "./bin/ditto.js"
33
+ "ditto-cli": "bin/ditto.js"
30
34
  },
31
35
  "devDependencies": {
32
36
  "@babel/core": "^7.11.4",
33
- "@babel/preset-env": "^7.11.0",
37
+ "@babel/preset-env": "^7.20.2",
38
+ "@babel/preset-typescript": "^7.18.6",
39
+ "@tsconfig/node16": "^1.0.3",
34
40
  "@types/jest": "^26.0.9",
35
- "babel-jest": "^26.3.0",
36
- "eslint": "^7.6.0",
41
+ "@types/js-yaml": "^4.0.5",
42
+ "@types/node": "^18.0.0",
43
+ "babel-jest": "^29.3.1",
44
+ "eslint": "^8.27.0",
37
45
  "eslint-config-airbnb-base": "^14.2.0",
38
- "eslint-plugin-import": "^2.22.0",
39
- "eslint-plugin-jest": "^23.20.0",
40
46
  "husky": "^7.0.4",
47
+ "jest": "^29.3.1",
41
48
  "lint-staged": "^11.2.4",
42
49
  "prettier": "2.4.1",
43
- "jest": "^27.3.1",
44
- "rewire": "^5.0.0",
50
+ "rewire": "^6.0.0",
45
51
  "source-map": "^0.7.3",
46
52
  "tempy": "^0.6.0",
47
- "tsconfig-paths": "^3.9.0",
48
- "typescript": "^4.0.2"
53
+ "typescript": "^4.7.4"
49
54
  },
50
55
  "dependencies": {
51
- "axios": "^0.19.2",
52
- "boxen": "^4.2.0",
56
+ "axios": "^0.27.2",
57
+ "boxen": "^5.1.2",
53
58
  "chalk": "^4.1.0",
54
59
  "commander": "^6.1.0",
55
60
  "enquirer": "^2.3.6",
56
61
  "faker": "^5.1.0",
57
- "js-yaml": "^3.14.0",
62
+ "js-yaml": "^4.1.0",
58
63
  "ora": "^5.0.0",
59
64
  "v8-compile-cache": "^2.1.1"
60
65
  },
File without changes
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "@tsconfig/node16/tsconfig.json",
3
+ "compilerOptions": {
4
+ "allowJs": true,
5
+ "outDir": "bin",
6
+ "allowSyntheticDefaultImports": true,
7
+ "resolveJsonModule": true,
8
+ "strictNullChecks": true,
9
+ "sourceMap": true
10
+ },
11
+ "include": ["lib/ditto.ts"]
12
+ }