Skip to content

Commit

Permalink
fix(init): handle symbolic links and ignored files properly
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Feb 6, 2020
1 parent 86d10ff commit 2d6b876
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions packages/agoric-cli/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default async function initMain(progname, rawArgs, priv) {
symlink,
readdir,
readFile,
readlink,
writeFile,
} = fs;

Expand All @@ -29,8 +30,11 @@ export default async function initMain(progname, rawArgs, priv) {
}
}

const templateDir = `${__dirname}/../template`;
const writeTemplate = async stem => {
const isIgnored = (suffix, name) =>
name === 'node_modules' ||
(suffix === '/.agservers' && name !== 'package.json' && name[0] !== '.');

const writeTemplate = async (templateDir, stem) => {
const template = await readFile(`${templateDir}${stem}`, 'utf-8');
const content = template
.replace(/['"]@DIR@['"]/g, JSON.stringify(DIR))
Expand All @@ -41,29 +45,42 @@ export default async function initMain(progname, rawArgs, priv) {
const recursiveTemplate = async (templateDir, suffix = '') => {
const cur = `${templateDir}${suffix}`;
const list = await readdir(cur);
await Promise.all(list.map(async name => {
if (name === 'node_modules' || name === 'solo') {
return;
}
const stem = `${suffix}/${name}`;
const st = await lstat(`${templateDir}${stem}`);
let target;
try {
target = await stat(`${DIR}${stem}`);
} catch (e) {}
if (st.isDirectory()) {
if (!target) {
console.log(`mkdir ${DIR}${stem}`);
await mkdir(`${DIR}${stem}`);
await Promise.all(
list.map(async name => {
const stem = `${suffix}/${name}`;
if (isIgnored(suffix, name)) {
return;
}
const st = await lstat(`${templateDir}${stem}`);
let target;
try {
target = await stat(`${DIR}${stem}`);
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
if (st.isDirectory()) {
if (!target) {
console.log(`mkdir ${DIR}${stem}`);
await mkdir(`${DIR}${stem}`);
}
await recursiveTemplate(templateDir, `${stem}`);
} else if (st.isSymbolicLink()) {
console.log(`symlink ${DIR}${stem}`);
await symlink(
await readlink(`${templateDir}${stem}`),
`${DIR}${stem}`,
);
} else {
console.log(`write ${DIR}${stem}`);
await writeTemplate(templateDir, stem);
}
await recursiveTemplate(templateDir, `${stem}`);
} else {
console.log(`write ${DIR}${stem}`);
await writeTemplate(stem);
}
}));
}),
);
};
await recursiveTemplate(templateDir);
await recursiveTemplate(`${__dirname}/../template`);

console.log(chalk.bold.yellow(`Done initializing`));
return 0;
}

0 comments on commit 2d6b876

Please sign in to comment.