From b0129604389b4851dc3fe5086c1d1127cf6e2e72 Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Wed, 1 Apr 2020 18:29:57 +0200 Subject: [PATCH] Add rimraf helper (#115) --- cli/builder.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cli/builder.js b/cli/builder.js index a66847a7..3bf685af 100755 --- a/cli/builder.js +++ b/cli/builder.js @@ -1,6 +1,5 @@ #!/usr/bin/env node const fs = require('fs') -const execSync = require('child_process').execSync const path = require('path') const getPageNamespaces = require('../_helpers/getPageNamespaces').default @@ -15,6 +14,24 @@ const { logBuild = true, } = require(process.cwd() + '/i18n.json') || {} +/** + * Similar to "rm -rf" + */ +function rimraf(pathname) { + if (!fs.existsSync(pathname)) return + + fs.readdirSync(pathname).forEach((child) => { + const childPathname = path.join(pathname, child) + + if (fs.lstatSync(childPathname).isDirectory()) { + rimraf(childPathname) + return + } + fs.unlinkSync(childPathname) + }) + fs.rmdirSync(pathname) +} + function readDirR(dir) { const parsedDir = dir.replace(/\\/g, '/') @@ -35,7 +52,7 @@ createPagesDir(allLanguages) * /pages/en/ - /pages/es/ ... */ async function createPagesDir(langs = []) { - execSync(`rm -rf ${finalPagesDir}`) + rimraf(finalPagesDir) fs.mkdirSync(finalPagesDir) langs.forEach(async (lang) => {