@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.
- package/README.md +150 -137
- package/bin/add-project.js +5 -7
- package/bin/add-project.js.map +1 -1
- package/bin/api.js +0 -5
- package/bin/api.js.map +1 -1
- package/bin/config.js +38 -11
- package/bin/config.js.map +1 -1
- package/bin/ditto.js +66 -57
- package/bin/ditto.js.map +1 -1
- package/bin/http/fetchVariants.js +26 -0
- package/bin/http/fetchVariants.js.map +1 -0
- package/bin/init/init.js +17 -6
- package/bin/init/init.js.map +1 -1
- package/bin/init/project.js +38 -45
- package/bin/init/project.js.map +1 -1
- package/bin/init/token.js +22 -20
- package/bin/init/token.js.map +1 -1
- package/bin/pull.js +142 -193
- package/bin/pull.js.map +1 -1
- package/bin/remove-project.js +2 -7
- package/bin/remove-project.js.map +1 -1
- package/bin/utils/cleanFileName.js +11 -0
- package/bin/utils/cleanFileName.js.map +1 -0
- package/bin/utils/generateJsDriver.js +56 -0
- package/bin/utils/generateJsDriver.js.map +1 -0
- package/bin/utils/getSelectedProjects.js +3 -18
- package/bin/utils/getSelectedProjects.js.map +1 -1
- package/bin/utils/projectsToText.js +10 -1
- package/bin/utils/projectsToText.js.map +1 -1
- package/bin/utils/promptForProject.js +2 -3
- package/bin/utils/promptForProject.js.map +1 -1
- package/bin/utils/quit.js +10 -0
- package/bin/utils/quit.js.map +1 -0
- package/lib/add-project.ts +6 -9
- package/lib/api.ts +0 -5
- package/lib/config.ts +57 -19
- package/lib/ditto.ts +74 -58
- package/lib/http/fetchVariants.ts +30 -0
- package/lib/init/init.ts +38 -6
- package/lib/init/project.test.ts +3 -3
- package/lib/init/project.ts +47 -58
- package/lib/init/token.ts +17 -16
- package/lib/pull.ts +205 -261
- package/lib/remove-project.ts +2 -8
- package/lib/types.ts +24 -3
- package/lib/utils/cleanFileName.ts +6 -0
- package/lib/utils/generateJsDriver.ts +68 -0
- package/lib/utils/getSelectedProjects.ts +5 -24
- package/lib/utils/projectsToText.ts +11 -1
- package/lib/utils/promptForProject.ts +2 -3
- package/lib/utils/quit.ts +5 -0
- package/package.json +1 -1
- 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
|
|
33
|
-
): Project[] => {
|
|
34
|
-
if (!fs.existsSync(configFile)) return [];
|
|
32
|
+
export const getSelectedProjects = (configFile = PROJECT_CONFIG_FILE) =>
|
|
33
|
+
Config.parseSourceInformation(configFile).validProjects;
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
|
|
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(
|
|
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
|
|
package/package.json
CHANGED