Skip to content

Instantly share code, notes, and snippets.

@wisetc
Created October 25, 2020 07:07
Show Gist options
  • Save wisetc/a0a4d8523bee4a13bc91dfb4fcd72593 to your computer and use it in GitHub Desktop.
Save wisetc/a0a4d8523bee4a13bc91dfb4fcd72593 to your computer and use it in GitHub Desktop.

Revisions

  1. wisetc created this gist Oct 25, 2020.
    66 changes: 66 additions & 0 deletions postinstall.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    const assert = require('assert');
    const {promisify} = require('util');
    const fs = require('fs');
    const {resolve} = require('path');

    //
    const symlink = promisify(fs.symlink);
    const stat = promisify(fs.stat);
    const mkdir = promisify(fs.mkdir);

    // Is path exists
    async function isExists(p) {
    try {
    await stat(p);
    return true;
    } catch (error) {
    return false;
    }
    }

    // ensure folder exists.
    async function ensureFolder(folder) {
    if (!(await isExists(folder))) {
    await mkdir(folder);
    console.log({folder});
    }
    }

    // ---

    const rootDir = resolve(__dirname, '..');
    const NODE_MODULES = resolve(rootDir, 'node_modules');

    const modulesMap = {
    '@wisetc/styledComponents': resolve(rootDir, '../testproject/dist'),
    }

    async function linkModules() {
    assert(process.platform === 'win32', 'Only windows platform support now.');
    const dirType = 'dir'; // 'junction'

    const promises = [];
    for (const module in modulesMap) {
    const target = modulesMap[module];
    const linkPath = resolve(NODE_MODULES, module);
    const folderName = resolve(linkPath, '..');
    await ensureFolder(folderName);
    console.log({linkPath, target});

    promises.push(symlink(target, linkPath, dirType));
    }

    const results = await Promise.all(promises);
    return results;
    }

    async function main() {
    try {
    await linkModules();
    console.log('done');
    } catch (error) {
    console.error(error.message);
    }
    }

    main();