Skip to content

Commit

Permalink
Merge pull request #2 from brian-dlee/bl-force-option
Browse files Browse the repository at this point in the history
add --force option to generate
  • Loading branch information
brian-dlee authored Aug 30, 2022
2 parents 64bb725 + bfe862d commit fbd6601
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"repository": {
"url": "https://github.com/brian-dlee/smuggler"
},
"version": "0.2.1",
"version": "0.2.2",
"description": "Smuggle large configuration variables into Vercel deployments in an encrypted format.",
"main": "./dist/index.js",
"bin": {
Expand Down
49 changes: 39 additions & 10 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface PrepareOptions extends Options {}

interface GenerateOptions extends Options {
empty: boolean;
force: boolean;
prepare: boolean;
}

Expand Down Expand Up @@ -297,21 +298,37 @@ async function generate(options: GenerateOptions) {
const config = await readConfig(options.config);

if (options.empty) {
if (!(await buildFileExists(config.buildLocation))) {
await writeEmptyLoaderFile(resolve(config.buildLocation, LOADER_FILENAME));
const fileExists = await buildFileExists(config.buildLocation);

if (options.force || fileExists) {
const loaderFilePath = resolve(config.buildLocation, LOADER_FILENAME);

if (options.force && fileExists) {
debugLogger('Overwriting loader file with empty loader (--force)');
} else if (!fileExists) {
debugLogger('The required loader files do not exist: %s', config.buildLocation);
}

await writeEmptyLoaderFile(loaderFilePath);
}
return;
}

if (!(await intermediateFileExists(config.intermediateLocation))) {
debugLogger('Intermediate files do not exist: %s', config.intermediateLocation);
const fileExists = await intermediateFileExists(config.intermediateLocation);

if (!options.prepare) {
console.error('Intermediate file does not exist and --no-prepare was supplied. Aborting.');
throw new Error('no intermediate file');
if ((options.force || fileExists) && options.prepare) {
if (options.force && fileExists) {
debugLogger('Overwriting intermediate files (--force)');
} else if (!fileExists) {
debugLogger('The required intermediate files do not exist: %s', config.intermediateLocation);
}

await generateIntermediateFile(config, options.empty ? {} : process.env);
} else if (!fileExists && !options.prepare) {
console.error(
'The required intermediate files do not exist and --no-prepare was supplied. Aborting.'
);
throw new Error('no intermediate file');
}

await generateBuildFile(config);
Expand Down Expand Up @@ -350,6 +367,17 @@ function attachBaseOptions(command: Command) {
return command;
}

function actionErrorHandler<T>(fn: (options: T) => Promise<void>) {
return async (options: T) => {
try {
await fn(options);
} catch (e) {
console.error(`${e}`);
process.exit(1);
}
};
}

const program = new Command('smuggler');

program.description('Smuggle encrypted files into your deployment');
Expand All @@ -358,23 +386,24 @@ attachBaseOptions(program.command('prepare'))
.description(
'Generate the encrypted data and store in an intermediate location in preparation for build.'
)
.action(prepare);
.action(actionErrorHandler(prepare));

attachBaseOptions(program.command('generate'))
.option(
'--empty',
'Generate an empty library (necessary for startup when using smuggler).',
false
)
.option('--force', 'Overwrite any existing smuggler library files')
.option('--no-prepare', 'Disable data preparation if the data does not already exist.')
.description(
'Generate the encrypted data, if it is not staged, and inject it into your application.'
)
.action(generate);
.action(actionErrorHandler(generate));

attachBaseOptions(program.command('read'))
.option('--contents', 'Display the contents of each variable.', false)
.description('Read a prepared smuggler file')
.action(read);
.action(actionErrorHandler(read));

program.parse();

1 comment on commit fbd6601

@vercel
Copy link

@vercel vercel bot commented on fbd6601 Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.