This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
-
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.
- Loading branch information
Showing
6 changed files
with
102 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env node | ||
|
||
import * as program from 'commander'; | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
import {copyJson} from './copyJson'; | ||
|
||
const defaultPackageJsonPath = path.join(__dirname, 'package.json'); | ||
const packageJsonPath = fs.existsSync(defaultPackageJsonPath) | ||
? defaultPackageJsonPath | ||
: path.join(__dirname, '../package.json'); | ||
|
||
const {bin, version} = fs.readJSONSync(packageJsonPath); | ||
const name = Object.keys(bin)[1]; | ||
|
||
program | ||
.name(name) | ||
.version(version) | ||
.description(`Copy entries from one JSON file to the other (example: ${name} version)`) | ||
.option('-i, --input <file>', 'Set the input JSON file', './flattened/package.json') | ||
.option('-o, --output <file>', 'Set the output JSON file', './package.json') | ||
.parse(process.argv); | ||
|
||
const values = program.args; | ||
|
||
if (!values.length) { | ||
console.error('No values to copy'); | ||
program.help(); | ||
} | ||
|
||
copyJson(program.input, program.output, values).catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
async function checkFile(filePath: string): Promise<void> { | ||
try { | ||
await fs.access(filePath, fs.constants.F_OK | fs.constants.R_OK); | ||
} catch (error) { | ||
throw new Error(`Input file "${filePath} doesn't exist or is not readable.`); | ||
} | ||
} | ||
|
||
export interface packageJson { | ||
[key: string]: string | packageJson; | ||
} | ||
|
||
export async function copyJson(inputFile: string, outputFile: string, values: string[]): Promise<void> { | ||
if (!values.length) { | ||
return; | ||
} | ||
|
||
const inputResolved = path.resolve(inputFile); | ||
const outputResolved = path.resolve(outputFile); | ||
|
||
await checkFile(inputResolved); | ||
await fs.ensureFile(outputResolved); | ||
|
||
const originalJSON: packageJson = await fs.readJSON(inputResolved); | ||
let newJSONRaw = await fs.readFile(outputResolved, 'utf-8'); | ||
|
||
let spaces = 2; | ||
|
||
if (!newJSONRaw) { | ||
console.log('no content'); | ||
newJSONRaw = '{}'; | ||
} else if (/^\{/.test(newJSONRaw)) { | ||
const spacesMatch = newJSONRaw.match(/^\{[\n\r]?( *)/); | ||
if (spacesMatch && spacesMatch[1]) { | ||
spaces = spacesMatch[1].length; | ||
} | ||
} | ||
|
||
const newJSON: packageJson = JSON.parse(newJSONRaw); | ||
|
||
for (const value of values) { | ||
if (value in originalJSON) { | ||
newJSON[value] = originalJSON[value]; | ||
} | ||
} | ||
|
||
await fs.writeJSON(outputResolved, newJSON, {spaces}); | ||
} |
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 +1,2 @@ | ||
export * from './PublishFlat'; | ||
export * from './copyJson'; |
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