From 0e36abfd5ce2276040f12ef33ea261fde7eb9520 Mon Sep 17 00:00:00 2001 From: Dan Delany Date: Tue, 19 Dec 2017 03:56:31 -0500 Subject: [PATCH] update npm scripts to build into root folder --- package.json | 3 ++- scripts/clean.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 scripts/clean.js diff --git a/package.json b/package.json index 7cf97539..677fe6bb 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ }, "scripts": { "dev": "webpack-dev-server --config webpack.config.base.js", - "build-lib": "rm -rf lib/ && babel src --out-dir lib --source-maps", + "clean": "node scripts/clean.js", + "build-lib": "npm run clean && babel src --out-dir ./ --source-maps", "build-docs": "BABEL_ENV=production webpack --config webpack.config.build.js", "build": "npm run build-lib && npm run make-docs && npm run build-docs", "make-docs": "node scripts/makeDocs.js", diff --git a/scripts/clean.js b/scripts/clean.js new file mode 100644 index 00000000..4fc963b8 --- /dev/null +++ b/scripts/clean.js @@ -0,0 +1,29 @@ +const fs = require("fs"); +const sh = require("shelljs"); + +const {fileExists, dirExists} = require("./utils"); + +const srcContents = sh.ls("src"); + +// We don't want to commit built files, so it is useful to have a `clean` script which deletes them if they exist. +// However, Reactochart is built in the root directory +// (so that modules may be required with eg. `require('reactochart/LineChart')`). +// This makes cleanup harder than simply deleting a `build` directory. +// Instead this looks for any files in the root directory which match the name of a file in the `src` directory, +// and deletes them if they exist. +// Sounds dangerous, but any files in root which share a name with src would have been overwritten by the build anyway. + +srcContents.forEach(fileOrDir => { + if (fileExists(`src/${fileOrDir}`) && fileExists(`./${fileOrDir}`)) { + console.log(`deleting file ./${fileOrDir}`); + sh.rm(`./${fileOrDir}`); + } else if (dirExists(`src/${fileOrDir}`) && dirExists(fileOrDir)) { + console.log(`deleting directory ./${fileOrDir}`); + sh.rm("-rf", `./${fileOrDir}`); + } + // check for source maps too + if(fileExists(`./${fileOrDir}.map`)) { + console.log(`deleting file ./${fileOrDir}.map`); + sh.rm(`./${fileOrDir}.map`); + } +});