Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Clean Dir Approach #804

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/usdk/lib/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ export const create = async (args, opts) => {
const source = args.source;
const features = typeof args.feature === 'string' ? JSON.parse(args.feature) : (args.feature || {});
const yes = args.yes;
const force = !!args.force;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this flag is no longer needed, we can remove the option from the cli.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes overlooked, removed

const noInstall = !!args.noInstall;
const forceNoConfirm = !!args.forceNoConfirm;
// opts
Expand Down Expand Up @@ -403,7 +402,6 @@ export const create = async (args, opts) => {
const _prepareDirectory = async () => {
console.log(pc.italic('Preparing directory...'));
await cleanDir(dstDir, {
force,
forceNoConfirm,
});
// bootstrap destination directory
Expand Down
42 changes: 19 additions & 23 deletions packages/usdk/lib/directory-util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { rimraf } from 'rimraf';
import pc from 'picocolors';
import { isYes } from './isYes.js'

export const cleanDir = async (dstDir, { force, forceNoConfirm } = {}) => {
export const cleanDir = async (dstDir, { forceNoConfirm } = {}) => {
const files = await (async () => {
try {
return await fs.promises.readdir(dstDir);
Expand All @@ -17,32 +17,28 @@ export const cleanDir = async (dstDir, { force, forceNoConfirm } = {}) => {
}
}
})();

if (files.length > 0) {
if (force || forceNoConfirm) {
if (!forceNoConfirm) {
const rl = readline.promises.createInterface({
input: process.stdin,
output: process.stdout,
});
if (!forceNoConfirm) {
const rl = readline.promises.createInterface({
input: process.stdin,
output: process.stdout,
});

const answer = await rl.question(`\nDelete the contents of "${path.resolve(dstDir)}"? ${pc.cyan('y/N')}: `)
rl.close();
console.log();
const answer = await rl.question(`\nDelete the contents of "${path.resolve(dstDir)}"? ${pc.cyan('y/N')}: `)
rl.close();
console.log();

if (!isYes(answer)) {
throw new Error('aborted');
}
if (!isYes(answer)) {
throw new Error('Create Aborted');
}

// Remove all files.
console.log(pc.italic('Removing old files...'));
await Promise.all(
files.map((filePath) => rimraf(path.join(dstDir, filePath))),
);
console.log(pc.italic('Removed old files...'));
} else {
// throw error
throw new Error('directory is not empty (-f to override)');
}

// Remove all files.
console.log(pc.italic('Removing old files...'));
await Promise.all(
files.map((filePath) => rimraf(path.join(dstDir, filePath))),
);
console.log(pc.italic('Removed old files...'));
}
};