UNPKG

msync

Version:

Easily manage building and syncing multiple node-modules in a flexibly defined workspace.

62 lines (60 loc) 1.82 kB
import * as Rsync from 'rsync'; import { fs } from './libs'; function rsyncExecute(rsync) { return new Promise((resolve, reject) => { rsync.execute((err, code, cmd) => { if (err) { reject(err); } else { resolve({ err, code, cmd }); } }); }); } export async function module(from, to) { const IGNORE = ['.DS_Store', 'node_modules', '.tmp']; const FROM_DIR = fs.join(from.dir, '/'); const TO_DIR = fs.join(to.dir, 'node_modules', from.name, '/'); await fs.ensureDir(TO_DIR); const rsync = new Rsync() .source(FROM_DIR) .destination(TO_DIR) .exclude(IGNORE) .delete() .flags('aW'); await rsyncExecute(rsync); } export async function logUpdate(target) { if (target.isIgnored || !target.tsconfig) { return; } const dir = fs.join(target.dir, target.tsconfig.compilerOptions.outDir || ''); const parentDir = fs.basename(fs.dirname(dir)); if (parentDir !== 'node_modules' || !(await fs.pathExists(dir))) { return; } const file = fs.join(dir, '.__msync.js'); const getTotal = async () => { if (!(await fs.pathExists(file))) { return 0; } const text = (await fs.readFile(file)).toString(); for (const line of text.split('\n')) { if (line.trim().startsWith('saveTotal')) { return parseInt(line.split(':')[1], 10) + 1; } } return 0; }; const total = await getTotal(); const text = ` /* TEMPORARY FILE GENERATED BY MSync (https://github.com/philcockfield/msync) This file causes [nodemon] to restart. It is safe to delete this file. saveTotal: ${total} */ `; await fs.writeFile(file, text); }