Skip to content

Commit

Permalink
refactor: move explainer into README (WICG#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres authored Feb 24, 2021
1 parent 0778ead commit 2f51353
Show file tree
Hide file tree
Showing 5 changed files with 7,448 additions and 144 deletions.
22 changes: 15 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

"use strict";
const fs = require("fs-promise");
const fs = require("fs").promises;
const git = require("./git");
const messages = require("./messages");
const path = require("path");
Expand Down Expand Up @@ -34,10 +34,10 @@ async function performGitTasks(collectedData) {
function populateTemplate(rawData, collectedData, file) {
// find all {{\w}} and replace them form collectedData
const replaceSet = (rawData.match(/{{\w+}}/gm) || [])
.map(match => match.replace(/[{{|}}]/g, ""))
.map((match) => match.replace(/[{{|}}]/g, ""))
.reduce((collector, match) => collector.add(match), new Set());
return Array.from(replaceSet)
.map(match => {
.map((match) => {
const key = new RegExp(`{{${match}}}`, "gm");
if (!collectedData[match]) {
console.warn(
Expand All @@ -62,8 +62,17 @@ async function getFilesToInclude(collectedData) {
}
const dirFiles = await fs.readdir(tmplDir);
return dirFiles
.filter(filename => !excludedFiles.has(filename))
.map(filename => [tmplDir + filename, `${process.cwd()}/${filename}`]);
.filter((filename) => !excludedFiles.has(filename))
.map((filename) => [tmplDir + filename, `${process.cwd()}/${filename}`]);
}

async function fileExists(filePath) {
try {
await fs.access(filePath, fs.F_OK);
} catch (err) {
return false;
}
return true;
}

// Uses git to get the name of the repo (cwd)
Expand All @@ -72,8 +81,7 @@ async function writeTemplates(collectedData) {
const destinations = await getFilesToInclude(collectedData);
const successfulWrites = [];
for (let [from, to] of destinations) {
const exists = await fs.exists(to);
if (exists) {
if (await fileExists(to)) {
console.warn(
`${y(" ⚠️ skipping")} ${gr(path.basename(to))} (already exists)`
);
Expand Down
Loading

0 comments on commit 2f51353

Please sign in to comment.