Skip to content

Commit

Permalink
Merge pull request #28 from treatwell/add-config-flag-to-override-def…
Browse files Browse the repository at this point in the history
…ault-location

feat: add option to override default config file location
  • Loading branch information
PaulGOUX27 authored Jul 9, 2024
2 parents e987fd7 + e01767c commit 1d432ed
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 78 deletions.
17 changes: 13 additions & 4 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';
import kleur from 'kleur';
import { Command, flags } from '@oclif/command';

import { MasterFile } from '../models';
import { MasterFileAlreadyExistsError } from '../errors';
import cli from 'cli-ux';
import kleur from 'kleur';

export default class Add extends Command {
static description = 'create and push a new master language file';

static args = [{ name: 'masterFile' }];

static flags = {
configPath: flags.string({
name: 'configPath',
required: false,
description: 'Path to wti-config.json file. If not provided, default to git root directory.'
})
};

static usage = '$ wti add locales/en/translation.json';

async run() {
const {
args: { masterFile },
flags: { configPath }
} = this.parse(Add);

if (masterFile) {
cli.action.start(`Adding ${masterFile} as a master file...`);

try {
await MasterFile.create(masterFile);
await MasterFile.create(masterFile, configPath);

cli.action.stop();

Expand Down
17 changes: 13 additions & 4 deletions src/commands/addLocale.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';
import kleur from 'kleur';
import { Command, flags } from '@oclif/command';

import { Locale } from '../models';
import cli from 'cli-ux';
import kleur from 'kleur';

export default class AddLocale extends Command {
static description = 'add a new locale to the project';

static args = [{ name: 'locale' }];

static flags = {
configPath: flags.string({
name: 'configPath',
required: false,
description: 'Path to wti-config.json file. If not provided, default to git root directory.'
})
};

static usage = '$ wti addLocale fr';

async run() {
const {
args: { locale },
flags: { configPath }
} = this.parse(AddLocale);

if (locale) {
cli.action.start(`Adding ${locale} as a locale...`);

try {
await new Locale(locale).create();
await new Locale(locale).create(configPath);

cli.action.stop(
`${kleur.green('[SUCCESS]')} Locale ${locale} is being added`
Expand Down
21 changes: 15 additions & 6 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';
import { Command, flags } from '@oclif/command';
import { getConfig, initialConfig, updateConfig } from '../helpers';

import { ConfigNotFoundError } from '../errors';
import { getConfig, updateConfig, initialConfig } from '../helpers';
import cli from 'cli-ux';

export default class Init extends Command {
static description = 'configure the project to sync with';

static flags = {
configPath: flags.string({
name: 'configPath',
required: false,
description: 'Path to wti-config.json file. If not provided, default to git root directory.'
})
};

async run() {
const { flags: { configPath } } = this.parse(Init);
let config = initialConfig;

try {
// get config object
config = await getConfig();
config = await getConfig(configPath);
} catch (err) {
// if it doesn't exist, create an empty one
if (err instanceof ConfigNotFoundError) {
await updateConfig(config);
await updateConfig(config, configPath);
}
} finally {
// if apiKey is set, ask if it's ok to override it
Expand All @@ -39,7 +48,7 @@ export default class Init extends Command {
project: {
apiKey,
},
});
}, configPath);

cli.action.stop();
}
Expand Down
21 changes: 15 additions & 6 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { Command } from '@oclif/command';
import kleur from 'kleur';
import Listr from 'listr';
import { Command, flags } from '@oclif/command';
import { MasterFile, Project } from '../models';

import { Project, MasterFile } from '../models';
import Listr from 'listr';
import kleur from 'kleur';

export default class Pull extends Command {
static description = 'pull target language file(s)';

static flags = {
configPath: flags.string({
name: 'configPath',
required: false,
description: 'Path to wti-config.json file. If not provided, default to git root directory.'
})
};

async run() {
const { projectFiles } = await Project.init();
const { flags: { configPath } } = this.parse(Pull);
const { projectFiles } = await Project.init(configPath);

let masterFile = projectFiles.filter(
(file) => file.master_project_file_id === null
Expand All @@ -20,7 +29,7 @@ export default class Pull extends Command {
(acc, current) =>
acc.concat({
title: `Pulling ${current.name}`,
task: async () => await new MasterFile(masterFileId).show(current),
task: async () => await new MasterFile(masterFileId).show(current, configPath),
}),
[] as Listr.ListrTask<Listr.ListrContext>[]
)
Expand Down
16 changes: 11 additions & 5 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cli from 'cli-ux';
import { Command, flags } from '@oclif/command';
import { MasterFile, Project } from '../models';

import { Project, MasterFile } from '../models';
import cli from 'cli-ux';

export default class Push extends Command {
static description = 'push master language file';
Expand All @@ -22,11 +22,16 @@ export default class Push extends Command {
// may be reversed with --no-ignore-missing to obsolete missing strings
allowNo: true,
}),
configPath: flags.string({
name: 'configPath',
required: false,
description: 'Path to wti-config.json file. If not provided, default to git root directory.'
})
};

async run() {
const { projectFiles } = await Project.init();
const { flags } = this.parse(Push);
const { flags : { configPath, ...flags } } = this.parse(Push);
const { projectFiles } = await Project.init(configPath);

let masterFile = projectFiles.filter(
(file) => file.master_project_file_id === null
Expand All @@ -37,7 +42,8 @@ export default class Push extends Command {
await new MasterFile(masterFile.id).update(
masterFile.name,
masterFile.locale_code,
flags
flags,
configPath
);

cli.action.stop(
Expand Down
19 changes: 14 additions & 5 deletions src/commands/rm.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { Command, flags } from '@oclif/command';
import { MasterFile, Project } from '../models';

import cli from 'cli-ux';
import { Command } from '@oclif/command';
import kleur from 'kleur';

import { MasterFile, Project } from '../models';

export default class Rm extends Command {
static description = 'delete a master language file from a project';

static args = [{ name: 'masterFile' }];

static flags = {
configPath: flags.string({
name: 'configPath',
required: false,
description: 'Path to wti-config.json file. If not provided, default to git root directory.'
})
};

static usage = '$ wti rm locales/en/translation.json';

async run() {
const {
args: { masterFile },
flags: { configPath }
} = this.parse(Rm);

// TODO: inquirer to list master language files
Expand All @@ -24,7 +33,7 @@ export default class Rm extends Command {

if (remove) {
cli.action.start(`Removing ${masterFile}..`);
const { projectMasterFiles } = await Project.init();
const { projectMasterFiles } = await Project.init(configPath);

const masterFileResult = projectMasterFiles.find(
(file) => file.name === masterFile
Expand All @@ -41,7 +50,7 @@ export default class Rm extends Command {
process.exit(1);
}

await new MasterFile(masterFileResult.id).remove();
await new MasterFile(masterFileResult.id).remove(configPath);

cli.action.stop();
}
Expand Down
17 changes: 13 additions & 4 deletions src/commands/rmLocale.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';
import kleur from 'kleur';
import { Command, flags } from '@oclif/command';

import { Locale } from '../models';
import cli from 'cli-ux';
import kleur from 'kleur';

export default class RmLocale extends Command {
static description = 'delete a locale from a project';

static args = [{ name: 'locale' }];

static flags = {
configPath: flags.string({
name: 'configPath',
required: false,
description: 'Path to wti-config.json file. If not provided, default to git root directory.'
})
};

static usage = '$ wti rmLocale fr';

async run() {
const {
args: { locale },
flags: { configPath }
} = this.parse(RmLocale);

if (locale) {
cli.action.start(`Removing ${locale} as a locale...`);

try {
await new Locale(locale).remove();
await new Locale(locale).remove(configPath);

cli.action.stop(
`${kleur.green('[SUCCESS]')} Locale ${locale} is being removed`
Expand Down
17 changes: 13 additions & 4 deletions src/commands/status.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import cli from 'cli-ux';
import { Command } from '@oclif/command';
import kleur from 'kleur';
import { Command, flags } from '@oclif/command';

import { Status as StatusModel } from '../models';
import cli from 'cli-ux';
import kleur from 'kleur';

export default class Status extends Command {
static description = 'fetch and display project statistics';

static flags = {
configPath: flags.string({
name: 'configPath',
required: false,
description: 'Path to wti-config.json file. If not provided, default to git root directory.'
})
};

static usage = '$ wti status';

async run() {
cli.action.start(`Loading statistics...`);
const { flags: { configPath } } = this.parse(Status);

try {
const stats = await new StatusModel().fetch();
const stats = await new StatusModel().fetch(configPath);
cli.action.stop();

const locales = Object.keys(stats);
Expand Down
33 changes: 20 additions & 13 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'fs';
import path from 'path';
import gitRootDir from 'git-root-dir';

import {
ConfigNotFoundError,
NotGitDirectoryError,
ConfigUpdateError,
NotGitDirectoryError,
} from '../errors';

import fs from 'fs';
import gitRootDir from 'git-root-dir';
import path from 'path';

interface Config {
project: {
apiKey: string;
Expand All @@ -22,24 +22,30 @@ export const initialConfig: Config = {

/**
* Return the path of the config file
*
* @param pathParam path to the config file if defined in the command. Overrides the default git root dir
*/
export const getConfigPath = async () => {
export const getConfigPath = async (pathParam?: string) => {
if (pathParam) {
return path.join(process.cwd(), pathParam);
}

const rootDir = await gitRootDir(process.cwd());

if (rootDir) {
const configFilePath = path.join(rootDir, CONFIG_NAME);

return Promise.resolve(configFilePath);
return path.join(rootDir, CONFIG_NAME);
}

throw new NotGitDirectoryError();
};

/**
* Return the config as a JSON object
*
* @param pathParam path to the config file if defined in the command. Overrides the default git root dir
*/
export const getConfig = async () => {
const configPath = await getConfigPath();
export const getConfig = async (pathParam?: string) => {
const configPath = await getConfigPath(pathParam);

if (fs.existsSync(configPath)) {
return (await require(configPath)) as Config;
Expand All @@ -52,9 +58,10 @@ export const getConfig = async () => {
* Update the config object
*
* @param newConfig new config object to write to file
* @param pathParam path to the config file if defined in the command. Overrides the default git root dir
*/
export const updateConfig = async (newConfig: Config) => {
const configPath = await getConfigPath();
export const updateConfig = async (newConfig: Config, pathParam?: string) => {
const configPath = await getConfigPath(pathParam);

fs.writeFile(configPath, JSON.stringify(newConfig, null, 2), (err) => {
if (err) throw new ConfigUpdateError(String(err));
Expand Down
Loading

0 comments on commit 1d432ed

Please sign in to comment.