Skip to content

Commit

Permalink
Provide CLI option for adding a file-level comment on the output file #1
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Aug 19, 2023
1 parent 0948f81 commit 630f495
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ i18next-resources-for-ts merge -i /Users/user/my/input -o /Users/user/my/output.
# i18next-resources-for-ts toc
#
# toc accepts also the optional -cts argument that will automatically convert json to ts files and the optional -d argument that will delete the original json files
# toc and interface accepts also the optional -c argument, that will add a file-level comment on the output file (i.e. -c "This file is generated by i18next-resources-for-ts")
```

*Make sure your folder structure contains all relevant namespaces (in your source/reference language):*
Expand Down
12 changes: 9 additions & 3 deletions bin/i18next-resources-for-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ if (cliArgs.length > 0 && cliArgs[1].indexOf('-') !== 0) {
inputPath = cliArgs[1]
outputPath = inputPath
}
let comment

const inputArgIndex = cliArgs.indexOf('-i')
const outputArgIndex = cliArgs.indexOf('-o')
const commentArgIndex = cliArgs.indexOf('-c')
if (inputArgIndex > -1 && cliArgs[inputArgIndex + 1]) inputPath = cliArgs[inputArgIndex + 1]
if (outputArgIndex > -1 && cliArgs[outputArgIndex + 1]) outputPath = cliArgs[outputArgIndex + 1]
if (commentArgIndex > -1 && cliArgs[commentArgIndex + 1]) comment = cliArgs[commentArgIndex + 1]

inputPath = path.resolve(inputPath)
outputPath = path.resolve(outputPath)

const commentSection = comment ? `/**\n * ${comment}\n */\n` : ''

const namespaces = getNamespaces(inputPath)

if (subCommand === 'toc') {
Expand All @@ -57,12 +62,12 @@ if (subCommand === 'toc') {
return n
})
nsToUse.forEach((n) => {
fs.writeFileSync(n.tsPath, n.ts, 'utf-8')
fs.writeFileSync(n.tsPath, commentSection + n.ts, 'utf-8')
})
}

const toc = tocForResources(nsToUse, outputFile)
fs.writeFileSync(outputFile, toc, 'utf-8')
fs.writeFileSync(outputFile, commentSection + toc, 'utf-8')
if (convertToTs && del) {
nsToUse.forEach((n) => {
fs.unlinkSync(n.path)
Expand All @@ -72,6 +77,7 @@ if (subCommand === 'toc') {
}

if (subCommand === 'merge') {
if (comment) console.warn('Comment is ignored for json file output.')
const merged = mergeResources(namespaces)
let outputFile = outputPath
if (!outputFile.endsWith('.json')) {
Expand All @@ -87,6 +93,6 @@ if (subCommand === 'interface') {
outputFile = path.join(outputFile, 'resources.d.ts')
}
const typeDefinitionFile = mergeResourcesAsInterface(namespaces)
fs.writeFileSync(outputFile, typeDefinitionFile, 'utf-8')
fs.writeFileSync(outputFile, commentSection + typeDefinitionFile, 'utf-8')
console.log(`created .d.ts resources file for ${namespaces.length} namespaces: ${outputFile}`)
}
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,35 @@
},
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.22.5"
"@babel/runtime": "^7.22.10"
},
"devDependencies": {
"@babel/core": "^7.22.5",
"@babel/plugin-transform-runtime": "^7.22.5",
"@babel/preset-env": "^7.22.5",
"@babel/core": "^7.22.10",
"@babel/plugin-transform-runtime": "^7.22.10",
"@babel/preset-env": "^7.22.10",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^25.0.1",
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-node-resolve": "^15.2.0",
"@rollup/plugin-terser": "0.4.3",
"@types/mocha": "^10.0.1",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"babel-plugin-add-module-exports": "^1.0.4",
"eslint": "^8.42.0",
"eslint": "^8.47.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-require-path-exists": "^1.1.9",
"eslint-plugin-standard": "^5.0.0",
"i18next": "^22.5.1",
"i18next-chained-backend": "^4.3.0",
"i18next": "^23.4.4",
"i18next-chained-backend": "^4.4.0",
"mocha": "^10.2.0",
"rollup": "^3.25.0",
"rollup": "^3.28.0",
"should": "^13.2.3",
"sinon": "^15.1.0",
"sinon": "^15.2.0",
"tsd": "^0.28.1",
"typescript": "^5.1.3"
"typescript": "^5.1.6"
},
"tsd": {
"directory": "test/types"
Expand Down

0 comments on commit 630f495

Please sign in to comment.