@dittowords/cli 2.7.1 → 3.0.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 (53) hide show
  1. package/README.md +150 -137
  2. package/bin/add-project.js +5 -7
  3. package/bin/add-project.js.map +1 -1
  4. package/bin/api.js +0 -5
  5. package/bin/api.js.map +1 -1
  6. package/bin/config.js +38 -11
  7. package/bin/config.js.map +1 -1
  8. package/bin/ditto.js +66 -57
  9. package/bin/ditto.js.map +1 -1
  10. package/bin/http/fetchVariants.js +26 -0
  11. package/bin/http/fetchVariants.js.map +1 -0
  12. package/bin/init/init.js +17 -6
  13. package/bin/init/init.js.map +1 -1
  14. package/bin/init/project.js +38 -45
  15. package/bin/init/project.js.map +1 -1
  16. package/bin/init/token.js +22 -20
  17. package/bin/init/token.js.map +1 -1
  18. package/bin/pull.js +142 -193
  19. package/bin/pull.js.map +1 -1
  20. package/bin/remove-project.js +2 -7
  21. package/bin/remove-project.js.map +1 -1
  22. package/bin/utils/cleanFileName.js +11 -0
  23. package/bin/utils/cleanFileName.js.map +1 -0
  24. package/bin/utils/generateJsDriver.js +56 -0
  25. package/bin/utils/generateJsDriver.js.map +1 -0
  26. package/bin/utils/getSelectedProjects.js +3 -18
  27. package/bin/utils/getSelectedProjects.js.map +1 -1
  28. package/bin/utils/projectsToText.js +10 -1
  29. package/bin/utils/projectsToText.js.map +1 -1
  30. package/bin/utils/promptForProject.js +2 -3
  31. package/bin/utils/promptForProject.js.map +1 -1
  32. package/bin/utils/quit.js +10 -0
  33. package/bin/utils/quit.js.map +1 -0
  34. package/lib/add-project.ts +6 -9
  35. package/lib/api.ts +0 -5
  36. package/lib/config.ts +57 -19
  37. package/lib/ditto.ts +74 -58
  38. package/lib/http/fetchVariants.ts +30 -0
  39. package/lib/init/init.ts +38 -6
  40. package/lib/init/project.test.ts +3 -3
  41. package/lib/init/project.ts +47 -58
  42. package/lib/init/token.ts +17 -16
  43. package/lib/pull.ts +205 -261
  44. package/lib/remove-project.ts +2 -8
  45. package/lib/types.ts +24 -3
  46. package/lib/utils/cleanFileName.ts +6 -0
  47. package/lib/utils/generateJsDriver.ts +68 -0
  48. package/lib/utils/getSelectedProjects.ts +5 -24
  49. package/lib/utils/projectsToText.ts +11 -1
  50. package/lib/utils/promptForProject.ts +2 -3
  51. package/lib/utils/quit.ts +5 -0
  52. package/package.json +1 -1
  53. package/tsconfig.json +2 -1
@@ -0,0 +1,68 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import consts from "../consts";
4
+ import output from "../output";
5
+ import { Source } from "../types";
6
+ import { cleanFileName } from "./cleanFileName";
7
+
8
+ // compatability with legacy method of specifying project ids
9
+ // that is still used by the default format
10
+ const stringifySourceId = (projectId: string) =>
11
+ projectId === "ditto_component_library" ? projectId : `project_${projectId}`;
12
+
13
+ /**
14
+ * Generates an index.js file that can be consumed
15
+ * by an SDK - this is a big DX improvement because
16
+ * it provides a single entry point to get all data
17
+ * (including variants) instead of having to import
18
+ * each generated file individually.
19
+ *
20
+ * The generated file will have a unified format
21
+ * independent of the CLI configuration used to fetch
22
+ * data from Ditto.
23
+ */
24
+
25
+ // TODO: support ESM
26
+ export function generateJsDriver(sources: Source[]) {
27
+ const fileNames = fs
28
+ .readdirSync(consts.TEXT_DIR)
29
+ .filter((fileName) => /\.json$/.test(fileName));
30
+
31
+ const sourceIdsByName: Record<string, string> = sources.reduce(
32
+ (agg, source) => {
33
+ if (source.fileName) {
34
+ return { ...agg, [cleanFileName(source.fileName)]: source.id };
35
+ }
36
+
37
+ return agg;
38
+ },
39
+ {}
40
+ );
41
+
42
+ const data = fileNames.reduce(
43
+ (obj: Record<string, Record<string, string>>, fileName) => {
44
+ const [sourceId, rest] = fileName.split("__");
45
+ const [variantApiId] = rest.split(".");
46
+
47
+ const projectId = sourceIdsByName[sourceId];
48
+ const projectIdStr = stringifySourceId(projectId);
49
+
50
+ if (!obj[projectIdStr]) {
51
+ obj[projectIdStr] = {};
52
+ }
53
+
54
+ obj[projectIdStr][variantApiId] = `require('./${fileName}')`;
55
+ return obj;
56
+ },
57
+ {}
58
+ );
59
+
60
+ let dataString = `module.exports = ${JSON.stringify(data, null, 2)}`
61
+ // remove quotes around require statements
62
+ .replace(/"require\((.*)\)"/g, "require($1)");
63
+
64
+ const filePath = path.resolve(consts.TEXT_DIR, "index.js");
65
+ fs.writeFileSync(filePath, dataString, { encoding: "utf8" });
66
+
67
+ return `Generated .js SDK driver at ${output.info(filePath)}`;
68
+ }
@@ -3,6 +3,7 @@ import yaml, { YAMLException } from "js-yaml";
3
3
 
4
4
  import { PROJECT_CONFIG_FILE } from "../consts";
5
5
  import { ConfigYAML, Project } from "../types";
6
+ import Config, { DEFAULT_CONFIG_JSON } from "../config";
6
7
 
7
8
  function jsonIsConfigYAML(json: unknown): json is ConfigYAML {
8
9
  return typeof json === "object";
@@ -28,28 +29,8 @@ function yamlToJson(_yaml: string): ConfigYAML | null {
28
29
  * Returns an array containing all valid projects ({ id, name })
29
30
  * currently contained in the project config file.
30
31
  */
31
- export const getSelectedProjects = (
32
- configFile = PROJECT_CONFIG_FILE
33
- ): Project[] => {
34
- if (!fs.existsSync(configFile)) return [];
32
+ export const getSelectedProjects = (configFile = PROJECT_CONFIG_FILE) =>
33
+ Config.parseSourceInformation(configFile).validProjects;
35
34
 
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
- };
35
+ export const getIsUsingComponents = (configFile = PROJECT_CONFIG_FILE) =>
36
+ Config.parseSourceInformation(configFile).shouldFetchComponentLibrary;
@@ -1,6 +1,16 @@
1
1
  import output from "../output";
2
2
  import { Project } from "../types";
3
3
 
4
+ export const getSourceUrl = (sourceId: string) => {
5
+ let base = "https://app.dittowords.com";
6
+
7
+ if (sourceId === "ditto_component_library") {
8
+ return `${base}/components`;
9
+ }
10
+
11
+ return `${base}/doc/${sourceId}`;
12
+ };
13
+
4
14
  const projectsToText = (projects: Project[]) => {
5
15
  return (
6
16
  (projects || []).reduce(
@@ -10,7 +20,7 @@ const projectsToText = (projects: Project[]) => {
10
20
  "- " +
11
21
  output.info(name) +
12
22
  " " +
13
- output.subtle("https://app.dittowords.com/doc/" + id)),
23
+ output.subtle(getSourceUrl(id))),
14
24
  ""
15
25
  ) + "\n"
16
26
  );
@@ -2,12 +2,11 @@ const { AutoComplete } = require("enquirer");
2
2
 
3
3
  import output from "../output";
4
4
  import { Project } from "../types";
5
+ import { getSourceUrl } from "./projectsToText";
5
6
 
6
7
  function formatProjectChoice(project: Project) {
7
8
  return (
8
- project.name +
9
- " " +
10
- output.subtle(project.url || `https://app.dittowords.com/doc/${project.id}`)
9
+ project.name + " " + output.subtle(project.url || getSourceUrl(project.id))
11
10
  );
12
11
  }
13
12
 
@@ -0,0 +1,5 @@
1
+ export function quit(message: string, exitCode = 2) {
2
+ console.log(`\n${message}\n`);
3
+ process.exitCode = exitCode;
4
+ process.exit();
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dittowords/cli",
3
- "version": "2.7.1",
3
+ "version": "3.0.0",
4
4
  "description": "Command Line Interface for Ditto (dittowords.com).",
5
5
  "main": "bin/index.js",
6
6
  "scripts": {
package/tsconfig.json CHANGED
@@ -6,7 +6,8 @@
6
6
  "allowSyntheticDefaultImports": true,
7
7
  "resolveJsonModule": true,
8
8
  "strictNullChecks": true,
9
- "sourceMap": true
9
+ "sourceMap": true,
10
+ "strict": true
10
11
  },
11
12
  "include": ["lib/ditto.ts"]
12
13
  }