generated from Pegase745/ts-node-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chore): add base commands for a first version
- add - addLocale - init - pull - push - rm - rmLocale - status
- Loading branch information
Showing
27 changed files
with
2,072 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
#!/usr/bin/env node | ||
|
||
require("../dist/index.js"); | ||
require('@oclif/command') | ||
.run() | ||
.then(require('@oclif/command/flush')) | ||
.catch(require('@oclif/errors/handle')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@echo off | ||
|
||
node "%~dp0\run" %* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import cli from 'cli-ux'; | ||
import { Command } from '@oclif/command'; | ||
import kleur from 'kleur'; | ||
|
||
import { MasterFile } from '../models'; | ||
import { MasterFileAlreadyExistsError } from '../errors'; | ||
|
||
export default class Add extends Command { | ||
static description = 'create and push a new master language file'; | ||
|
||
static args = [{ name: 'masterFile' }]; | ||
|
||
static usage = '$ wti add locales/en/translation.json'; | ||
|
||
async run() { | ||
const { | ||
args: { masterFile }, | ||
} = this.parse(Add); | ||
|
||
if (masterFile) { | ||
cli.action.start(`Adding ${masterFile} as a master file...`); | ||
|
||
try { | ||
await MasterFile.create(masterFile); | ||
|
||
cli.action.stop(); | ||
|
||
this.log( | ||
`${kleur.green('[SUCCESS]')} Master file ${masterFile} is being added` | ||
); | ||
} catch (err) { | ||
cli.action.stop(); | ||
|
||
if (err instanceof MasterFileAlreadyExistsError) { | ||
this.log( | ||
`${kleur.red('[ERROR]')} Master file ${masterFile} already exist` | ||
); | ||
} | ||
} | ||
} else { | ||
this.log(`${kleur.red('[ERROR]')} Please specify a master file to add`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import cli from 'cli-ux'; | ||
import { Command } from '@oclif/command'; | ||
import kleur from 'kleur'; | ||
|
||
import { Locale } from '../models'; | ||
|
||
export default class AddLocale extends Command { | ||
static description = 'add a new locale to the project'; | ||
|
||
static args = [{ name: 'locale' }]; | ||
|
||
static usage = '$ wti addLocale fr'; | ||
|
||
async run() { | ||
const { | ||
args: { locale }, | ||
} = this.parse(AddLocale); | ||
|
||
if (locale) { | ||
cli.action.start(`Adding ${locale} as a locale...`); | ||
|
||
try { | ||
await new Locale(locale).create(); | ||
|
||
cli.action.stop( | ||
`${kleur.green('[SUCCESS]')} Locale ${locale} is being added` | ||
); | ||
} catch (err) { | ||
cli.action.stop(`${kleur.red('[ERROR]')} ${err}`); | ||
} | ||
} else { | ||
this.log(`${kleur.red('[ERROR]')} Please specify a locale to add`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import cli from 'cli-ux'; | ||
import { Command } from '@oclif/command'; | ||
|
||
import { ConfigNotFoundError } from '../errors'; | ||
import { getConfig, updateConfig, initialConfig } from '../helpers'; | ||
|
||
export default class Init extends Command { | ||
static description = 'configure the project to sync with'; | ||
|
||
async run() { | ||
let config = initialConfig; | ||
|
||
try { | ||
// get config object | ||
config = await getConfig(); | ||
} catch (err) { | ||
// if it doesn't exist, create an empty one | ||
if (err instanceof ConfigNotFoundError) { | ||
await updateConfig(config); | ||
} | ||
} finally { | ||
// if apiKey is set, ask if it's ok to override it | ||
if (config.project.apiKey) { | ||
const override = await cli.confirm( | ||
'You already have a project configured. Do you want to override it? (yes/no)' | ||
); | ||
|
||
// exit in case of no override | ||
if (!override) this.exit(); | ||
} | ||
|
||
// or else ask for apiKey and save it | ||
const apiKey = await cli.prompt("What is your project's api key?"); | ||
|
||
cli.action.start('Initializing...'); | ||
|
||
await updateConfig({ | ||
...config, | ||
project: { | ||
apiKey, | ||
}, | ||
}); | ||
|
||
cli.action.stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Command } from '@oclif/command'; | ||
import kleur from 'kleur'; | ||
import Listr from 'listr'; | ||
|
||
import { Project, MasterFile } from '../models'; | ||
|
||
export default class Pull extends Command { | ||
static description = 'pull target language file(s)'; | ||
|
||
async run() { | ||
const { projectFiles } = await Project.init(); | ||
|
||
let masterFile = projectFiles.filter( | ||
(file) => file.master_project_file_id === null | ||
)[0], | ||
masterFileId = masterFile.id; | ||
|
||
const tasks = new Listr( | ||
projectFiles.reduce( | ||
(acc, current) => | ||
acc.concat({ | ||
title: `Pulling ${current.name}`, | ||
task: async () => await new MasterFile(masterFileId).show(current), | ||
}), | ||
[] as Listr.ListrTask<Listr.ListrContext>[] | ||
) | ||
); | ||
|
||
tasks.run().catch((err) => { | ||
this.log(`${kleur.red('[ERROR]')} ${err}`); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import cli from 'cli-ux'; | ||
import { Command } from '@oclif/command'; | ||
|
||
import { Project, MasterFile } from '../models'; | ||
|
||
export default class Push extends Command { | ||
static description = 'push master language file'; | ||
|
||
async run() { | ||
const { projectFiles } = await Project.init(); | ||
|
||
let masterFile = projectFiles.filter( | ||
(file) => file.master_project_file_id === null | ||
)[0]; | ||
|
||
cli.action.start(`Pushing master file ${masterFile.name}...`); | ||
|
||
await new MasterFile(masterFile.id).update( | ||
masterFile.name, | ||
masterFile.locale_code | ||
); | ||
|
||
cli.action.stop( | ||
'done. Note that it can take up to 1 min for 1000 segments to be uploaded' | ||
); | ||
} | ||
} |
Oops, something went wrong.