Skip to content

Commit

Permalink
feat(tests): added script to copy htmls
Browse files Browse the repository at this point in the history
A simplistic script is added to watch and copy files.
Existing packages like cpx does not copy copy new file.
  • Loading branch information
Sayan751 committed Oct 8, 2019
1 parent aaefd34 commit 823833d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/__tests__/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
"test-chrome:watch": "karma start karma.conf.js --browsers=ChromeHeadlessOpt --coverage --watch-extensions js",
"test-chrome:verbose": "karma start karma.conf.js --browsers=ChromeHeadlessOpt --single-run --coverage --reporter=mocha",
"test-chrome:watch:verbose": "karma start karma.conf.js --browsers=ChromeHeadlessOpt --coverage --watch-extensions js --reporter=mocha",
"test-chrome:verbose": "karma start karma.conf.js --browsers=ChromeHeadlessOpt --single-run --coverage --reporter=mocha",
"test-chrome:debugger": "karma start karma.conf.js --browsers=ChromeDebugging --watch-extensions js",
"test-chrome:debugger:verbose": "karma start karma.conf.js --browsers=ChromeDebugging --watch-extensions js --reporter=mocha",
"copy-html": "cpx ./integration/**/*.html ./dist/esnext/__tests__/integration -v",
"build": "concurrently npm:copy-html \"tsc -b\"",
"dev": "concurrently \"npm run copy-html -- --w\" \"tsc -b -w --preserveWatchOutput\""
"copy-html": "node tasks/copy-html --verbose",
"build": "concurrently \"tsc -b\" \"npm:copy-html\"",
"dev": "concurrently \"tsc -b -w --preserveWatchOutput\" \"npm run copy-html -- --watch\""
},
"dependencies": {
"@aurelia/aot": "0.3.0",
Expand Down Expand Up @@ -63,8 +62,8 @@
"@types/webpack-env": "^1.14.0",
"@types/vinyl": "^2.0.3",
"chalk": "^2.4.2",
"chokidar": "^3.0.2",
"concurrently": "^4.1.1",
"cpx": "^1.5.0",
"cross-env": "^6.0.2",
"esm": "^3.2.25",
"full-icu": "^1.3.0",
Expand Down Expand Up @@ -93,6 +92,7 @@
"tslib": "^1.10.0",
"typescript": "^3.6.3",
"vinyl": "^2.2.0",
"webpack": "^4.41.0"
"webpack": "^4.41.0",
"yargs": "^13.3.0"
}
}
83 changes: 83 additions & 0 deletions packages/__tests__/tasks/copy-html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// @ts-check
/**
* A minimalist implementation to watch and copy html files to dist.
* Existing packages like cpx does not copy new files.
*/
const chokidar = require('chokidar');
const argv = require('yargs').argv;
const path = require('path');
const fs = require('fs');

const htmlSourceDirs = ['integration'];
const baseOutDir = 'dist/esnext/__tests__';
const toWatch = argv.watch;
const verbose = argv.watch;
const cwd = process.cwd();

/**
* @param {string} msg
*/
function log(msg) {
if (verbose) {
console.log(msg);
}
}

/**
* @param {string} src
* @param {string} dest
*/
function copyFile(src, dest) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(path.dirname(dest), {
recursive: true
});
}
fs.copyFileSync(src, dest, fs.constants.COPYFILE_FICLONE);
}


/**
* @param {string} dest
*/
function deleteFile(dest) {
let dir = path.dirname(dest);
fs.unlinkSync(dest);
while (fs.readdirSync(dir).length < 1 && dir !== cwd) {
fs.rmdirSync(dir);
dir = path.join(dir, '..');
}
}

/**
* @param {string} eventName
* @param {string} src
*/
function handleChange(eventName, src) {
const dest = path.join(cwd, baseOutDir, path.relative(cwd, src));

log(`${eventName}:${dest}`);

switch (eventName) {
case 'add':
case 'change':
copyFile(src, dest);
break;
case 'unlink':
deleteFile(dest);
break;
default:
break;
}
}

const watched = htmlSourceDirs.map((dir) => path.join(cwd, dir, '**/*.html'));

const watcher = chokidar.watch(watched);
watcher.on('all', handleChange);
if (!toWatch) {
watcher.on('ready', () => {
watcher.unwatch(watched);
watcher.close();
});
}

0 comments on commit 823833d

Please sign in to comment.