Skip to content

Commit 3bd2af5

Browse files
authored
[DIT-6730] Add disableJsDriver option (#109)
* feat: add config option for disabling js driver file generation * test: add test validating new property * chore: version bump * oops --------- Co-authored-by: Jordin Gardner <[email protected]>
1 parent 0932396 commit 3bd2af5

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

lib/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as Sentry from "@sentry/node";
66

77
import consts from "./consts";
88
import { createSentryContext } from "./utils/createSentryContext";
9-
import { Project, ConfigYAML, SourceInformation } from "./types";
9+
import { Project, ConfigYAML, SourceInformation, Source } from "./types";
1010

1111
export const DEFAULT_CONFIG_JSON: ConfigYAML = {
1212
sources: {
@@ -198,6 +198,7 @@ function parseSourceInformation(file?: string): SourceInformation {
198198
iosLocales,
199199
projects: projectsRoot,
200200
components: componentsRoot,
201+
disableJsDriver,
201202
} = readProjectConfigData(file);
202203

203204
const projects = sources?.projects || [];
@@ -239,7 +240,7 @@ function parseSourceInformation(file?: string): SourceInformation {
239240
*/
240241
const hasSourceData = !!validProjects.length || shouldFetchComponentLibrary;
241242

242-
const result = {
243+
const result: SourceInformation = {
243244
hasSourceData,
244245
validProjects,
245246
shouldFetchComponentLibrary,
@@ -255,6 +256,7 @@ function parseSourceInformation(file?: string): SourceInformation {
255256
localeByVariantApiId: iosLocales
256257
? iosLocales.reduce((acc, e) => ({ ...acc, ...e }), {} as any)
257258
: undefined,
259+
disableJsDriver,
258260
};
259261

260262
Sentry.setContext("config", createSentryContext(result));

lib/pull.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,44 @@ describe("pull", () => {
7474

7575
expect(filesOnDiskExpected.size).toBe(0);
7676
});
77+
78+
it("correctly does not write index.js or index.d.ts when `disableJsDriver: true` is specified", async () => {
79+
process.env.DITTO_TEXT_DIR = "/ditto";
80+
process.env.DITTO_PROJECT_CONFIG_FILE = "/ditto/config.yml";
81+
82+
// we need to manually mock responses for the http calls that happen
83+
// directly within the pull function; we don't need to mock the http
84+
// calls that happen by way of http/* function calls since those have
85+
// their own mocks already.
86+
axiosMock.get.mockImplementation(
87+
(): Promise<any> => Promise.resolve({ data: "data" })
88+
);
89+
90+
vol.fromJSON({
91+
[consts.CONFIG_FILE]: mockGlobalConfigFile,
92+
[consts.PROJECT_CONFIG_FILE]:
93+
mockProjectConfigFile + "\n" + "disableJsDriver: true",
94+
});
95+
96+
await pull();
97+
98+
const filesOnDiskExpected = new Set([
99+
"components__example-folder__base.json",
100+
"components__example-folder__example-variant-1.json",
101+
"components__example-folder__example-variant-2.json",
102+
"components__root__base.json",
103+
"components__root__example-variant-1.json",
104+
"components__root__example-variant-2.json",
105+
"test-project__base.json",
106+
"test-project__example-variant-1.json",
107+
"test-project__example-variant-2.json",
108+
]);
109+
110+
const filesOnDisk = fs.readdirSync("/ditto");
111+
filesOnDisk.forEach((file) => {
112+
filesOnDiskExpected.delete(file);
113+
});
114+
115+
expect(filesOnDiskExpected.size).toBe(0);
116+
});
77117
});

lib/pull.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ function cleanOutputFiles() {
286286

287287
if (
288288
item.isFile() &&
289-
/\.js(on)?|\.xml|\.strings(dict)?$|\.swift$/.test(item.name)
289+
/\.js(on)?|\.ts|\.xml|\.strings(dict)?$|\.swift$/.test(item.name)
290290
) {
291291
return fs.unlinkSync(path.resolve(consts.TEXT_DIR, item.name));
292292
}
@@ -310,6 +310,7 @@ async function downloadAndSave(
310310
componentFolders: specifiedComponentFolders,
311311
componentRoot,
312312
localeByVariantApiId,
313+
disableJsDriver,
313314
} = source;
314315

315316
const formats = getFormat(formatFromSource);
@@ -525,7 +526,8 @@ async function downloadAndSave(
525526

526527
const sources: Source[] = [...validProjects, ...componentSources];
527528

528-
if (hasJSONFormat) msg += generateJsDriver(sources, getJsonFormat(formats));
529+
if (hasJSONFormat && !disableJsDriver)
530+
msg += generateJsDriver(sources, getJsonFormat(formats));
529531

530532
if (shouldGenerateIOSBundles) {
531533
msg += "iOS locale information detected, generating bundles..\n\n";

lib/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export interface ConfigYAML {
5252
// TODO: might want to rename this at some point
5353
iosLocales?: Record<string, string>[];
5454

55+
// prevents the generation of index.js and index.d.ts files
56+
// when working with JSON formats
57+
disableJsDriver?: boolean;
58+
5559
// these are legacy fields - if they exist, we should output
5660
// a deprecation error, and suggest that they nest them under
5761
// a top-level `sources` property
@@ -73,6 +77,7 @@ export interface SourceInformation {
7377
componentRoot: boolean | { status: string } | undefined;
7478
componentFolders: ComponentFolder[] | undefined;
7579
localeByVariantApiId: Record<string, string> | undefined;
80+
disableJsDriver?: boolean;
7681
}
7782

7883
export type Token = string | undefined;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dittowords/cli",
3-
"version": "4.4.1",
3+
"version": "4.5.0",
44
"description": "Command Line Interface for Ditto (dittowords.com).",
55
"license": "MIT",
66
"main": "bin/index.js",

0 commit comments

Comments
 (0)