Skip to content

Commit

Permalink
Move to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Jan 10, 2020
1 parent 63742cb commit 2509eda
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/

# next.js build output
.next

dist/
2 changes: 1 addition & 1 deletion bin/watch-module.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require('../src/index.js');
require('../dist/index.js');
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"bin": {
"watch-module": "bin/watch-module.js"
},
"scripts": {
"build": "tsc --build tsconfig.json",
"prepublishOnly": "tsc --build tsconfig.json"
},
"repository": "https://github.com/mapado/watch-module",
"license": "MIT",
"dependencies": {
Expand All @@ -14,5 +18,12 @@
"fs-extra": "^8.1.0",
"has-yarn": "^2.1.0",
"minimist": "^1.2.0"
},
"devDependencies": {
"@types/debounce": "^1.2.0",
"@types/fs-extra": "^8.0.0",
"@types/minimist": "^1.2.0",
"@types/node": "^12.7.2",
"typescript": "^3.5.3"
}
}
71 changes: 34 additions & 37 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const { exec } = require('child_process');
const fs = require('fs-extra');
const debounce = require('debounce');
const chokidar = require('chokidar');
const minimist = require('minimist');
const chalk = require('chalk');
const process = require('process');
const hasYarn = require('has-yarn');

const theme = {
success: '#a9dc76',
moduleName: '#ab9df2',
date: '#808080',
debug: '#ffd866',
import { exec } from 'child_process';
import fs from 'fs-extra';
import nodeProcess from 'process';
import debounce from 'debounce';
import chokidar from 'chokidar';
import minimist from 'minimist';
import chalk from 'chalk';
import hasYarn from 'has-yarn';

enum Theme {
success = '#a9dc76',
moduleName = '#ab9df2',
date = '#808080',
debug = '#ffd866',

// colors are taken from https://www.monokai.pro/
// magenta: '#ff6188',
Expand All @@ -23,35 +23,35 @@ const theme = {
};

/* ================== logging ================== */
const cwd = process.cwd();
const argv = minimist(process.argv.slice(2));
const cwd = nodeProcess.cwd();
const argv = minimist(nodeProcess.argv.slice(2)).filter((a: string|undefined) => a);

const logDate = () => chalk.hex(theme.date)(`[${new Date().toISOString()}]`);
const logDate = () => chalk.hex(Theme.date)(`[${new Date().toISOString()}]`);

const debug = (...args) => {
const debug = (...args: string[]) => {
if (argv.v || argv.verbose) {
console.debug(logDate(), chalk.hex(theme.debug)('DEBUG'), ...args);
console.debug(logDate(), chalk.hex(Theme.debug)('DEBUG'), ...args);
}
}

const log = (...args) => {
const log = (...args: string[]) => {
console.log(logDate(), ...args);
}

const logModuleName = chalk.hex(theme.moduleName);
const logModuleName = chalk.hex(Theme.moduleName);

debug('arguments: ', argv);

const moduleNameByPath = {};
function getModuleNameForPath(path) {
const moduleNameByPath: {[key:string]: string} = {};
function getModuleNameForPath(path: string): string {
if (!moduleNameByPath[path]) {
moduleNameByPath[path] = require(`${cwd}/${path}/package.json`).name;
}

return moduleNameByPath[path];
}

function getModuleCommandForPath(path) {
function getModuleCommandForPath(path: string): string {
const packageJson = require(`${cwd}/${path}/package.json`);
if (packageJson['watch-module'] && packageJson['watch-module']['command']) {
return packageJson['watch-module']['command'];
Expand All @@ -64,13 +64,13 @@ function getModuleCommandForPath(path) {
}

/* ================== build ================== */
const changedModules = new Set();
function buildAll() {
const changedModules: Set<string> = new Set();
function buildAll(): void {
changedModules.forEach(buildPath);
changedModules.clear();
}

function buildPath(path) {
function buildPath(path: string): void {
const moduleName = getModuleNameForPath(path);
log(logModuleName(moduleName), `Change detected`);

Expand All @@ -95,7 +95,7 @@ function buildPath(path) {
return fs.ensureDir(modulePath)
.then(() =>
fs.copy(path, modulePath, {
filter: (src, dest) => {
filter: (src: string, dest: string) => {
const srcAppendSlash = `${src}/`;

return (
Expand All @@ -106,10 +106,7 @@ function buildPath(path) {
})
)
.then(() => {
log(
logModuleName(moduleName),
chalk.hex(theme.success)('build done')
);
log(logModuleName(moduleName), chalk.hex(Theme.success)('build done'));
})
.catch(console.error);
}
Expand All @@ -119,27 +116,27 @@ function buildPath(path) {
/* ================== debounce & events ================== */
const debouncedOnChangeAll = debounce(buildAll, 200);

function onChange(modulePath) {
function onChange(modulePath: string): void {
changedModules.add(modulePath);
debouncedOnChangeAll();
}

/* ================== main ================== */
function main() {
function main(): void {
const modulePaths = argv._;

if (modulePaths.length === 0) {
console.error("You must specify a module's path !!!");
return;
}

const srcPaths = modulePaths.map(path => `${path}/src`);
const srcPaths = modulePaths.map((path: string) => `${path}/src`);

// One-liner for current directory, ignores .dotfiles
chokidar
.watch(srcPaths, { ignored: /(^|[\/\\])\.[^\.\/]/ })
.on('all', (event, path) => {
const modulePath = modulePaths.find(tmpPath => path.startsWith(`${tmpPath}/`));
.on('all', (event: any, path: string) => {
const modulePath = modulePaths.find((tmpPath: string) => path.startsWith(`${tmpPath}/`));

onChange(modulePath);
});
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"strict": true,
"moduleResolution": "node",
"module": "commonjs",
"esModuleInterop": true,
"target": "es5",
"allowJs": true

},
"include": [
"./src/"
]
}
27 changes: 27 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
# yarn lockfile v1


"@types/debounce@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/debounce/-/debounce-1.2.0.tgz#9ee99259f41018c640b3929e1bb32c3dcecdb192"
integrity sha512-bWG5wapaWgbss9E238T0R6bfo5Fh3OkeoSt245CM7JJwVwpw6MEBCbIxLq5z8KzsE3uJhzcIuQkyiZmzV3M/Dw==

"@types/fs-extra@^8.0.0":
version "8.0.0"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-8.0.0.tgz#d3e2c313ca29f95059f198dd60d1f774642d4b25"
integrity sha512-bCtL5v9zdbQW86yexOlXWTEGvLNqWxMFyi7gQA7Gcthbezr2cPSOb8SkESVKA937QD5cIwOFLDFt0MQoXOEr9Q==
dependencies:
"@types/node" "*"

"@types/minimist@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=

"@types/node@*", "@types/node@^12.7.2":
version "12.7.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.2.tgz#c4e63af5e8823ce9cc3f0b34f7b998c2171f0c44"
integrity sha512-dyYO+f6ihZEtNPDcWNR1fkoTDf3zAK3lAABDze3mz6POyIercH0lEUawUFXlG8xaQZmm1yEBON/4TsYv/laDYg==

ansi-styles@^3.2.1:
version "3.2.1"
resolved "http://npm.mapado.net:4873/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
Expand Down Expand Up @@ -191,6 +213,11 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

typescript@^3.5.3:
version "3.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==

universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
Expand Down

0 comments on commit 2509eda

Please sign in to comment.