Skip to content

Commit

Permalink
Merged release/1.4.0 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiko Mathes committed Dec 20, 2016
2 parents 09b28db + a9107b6 commit 65e15ef
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
99 changes: 82 additions & 17 deletions lib/minstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const os = require('os');
const path = require('path');
const Promise = require('bluebird');
const readline = require('readline');
const cwd = process.cwd();

const UncriticalError = require('./UncriticalError.js');
const systools = require('./systools.js');
Expand All @@ -15,6 +16,10 @@ const ModuleInfo = require('./ModuleInfo');
let commandConcatSymbol = ';';
let isInProjectRoot = true;

let installedAsDependency = false;
let dependencyInstallLinkedFolders = [];
let projectFolderName = null;

function logIfInRoot(message) {
if (isInProjectRoot) {
console.log(message);
Expand All @@ -23,7 +28,7 @@ function logIfInRoot(message) {

function logVersionConflict(messages, moduleFolderName) {

const moduleFolder = path.join(process.cwd(), moduletools.modulesFolder, moduleFolderName);
const moduleFolder = path.join(cwd, moduletools.modulesFolder, moduleFolderName);
console.log(`
| UNCRITICAL DEPENDENCY-CONFLICTS FOUND:
Expand All @@ -42,25 +47,40 @@ function getInstalledModules() {
}

function checkStartConditions() {
return systools.verifyFolderName(process.cwd(), 'node_modules')
return systools.verifyFolderName(cwd, 'node_modules')
.then((folderName) => {
if (folderName == null) {
throw new UncriticalError('minstall started from outside the project-root. aborting.');

const pathParts = cwd.split(path.sep);
if (pathParts[pathParts.length - 2] !== 'node_modules') {
throw new UncriticalError('minstall started from outside the project-root. aborting.');
} else {
installedAsDependency = true;
}
}

return systools.verifyFolderName(process.cwd(), moduletools.modulesFolder);
return systools.verifyFolderName(cwd, moduletools.modulesFolder);
})
.then((folderName) => {
if (folderName == null) {
throw new UncriticalError(`${moduletools.modulesFolder} not found, thus minstall is done :)`);
}

if (installedAsDependency) {
return;
}

return Promise.all([
systools.isSymlink(path.join(process.cwd(), 'node_modules')),
systools.isSymlink(path.join(cwd, 'node_modules')),
getLocalPackageInfo(),
]);
})
.then((results) => {

if (installedAsDependency) {
return;
}

if (isInProjectRoot) {
isInProjectRoot = !results[0];
}
Expand All @@ -72,19 +92,19 @@ function checkStartConditions() {
}

function linkModuleToRoot(moduleFolder) {
const targetPath = path.join(process.cwd(), 'node_modules', moduleFolder);
const modulePath = path.join(process.cwd(), moduletools.modulesFolder, moduleFolder);
const targetPath = path.join(cwd, 'node_modules', moduleFolder);
const modulePath = path.join(cwd, moduletools.modulesFolder, moduleFolder);
return systools.link(modulePath, targetPath);
}

function linkRootToModule(moduleFolder) {
const rootModuleFolder = path.join(process.cwd(), 'node_modules');
const targetFolder = path.join(process.cwd(), moduletools.modulesFolder, moduleFolder, 'node_modules');
const rootModuleFolder = path.join(cwd, 'node_modules');
const targetFolder = path.join(cwd, moduletools.modulesFolder, moduleFolder, 'node_modules');
return systools.link(rootModuleFolder, targetFolder);
}

function installMissingModuleDependencies(moduleFolder, missingDependencies) {
const moduleNodeModules = path.join(process.cwd(), moduletools.modulesFolder, moduleFolder, 'node_modules');
const moduleNodeModules = path.join(cwd, moduletools.modulesFolder, moduleFolder, 'node_modules');
return systools.delete(moduleNodeModules)
.then(() => {
return moduletools.installPackets(missingDependencies);
Expand All @@ -96,7 +116,7 @@ function runPostinstall(moduleInfo) {
return Promise.resolve();
}

const modulePath = path.join(process.cwd(), moduletools.modulesFolder, moduleInfo.folderName);
const modulePath = path.join(cwd, moduletools.modulesFolder, moduleInfo.folderName);
return linkRootToModule(moduleInfo.folderName)
.then(() => {

Expand All @@ -122,7 +142,7 @@ function cacheConflictingModules(conflictedDependencies, moduleFolderName) {
return dependency.folderName;
});

const rootNodeModules = path.join(process.cwd(), 'node_modules');
const rootNodeModules = path.join(cwd, 'node_modules');
const cacheFolder = path.join(rootNodeModules, `${moduleFolderName}_cache`);
return systools.moveMultiple(conflictedDependencyFolders, rootNodeModules, cacheFolder);
}
Expand All @@ -137,9 +157,9 @@ function uncacheConflictingModules(conflictedDependencies, moduleFolderName) {
return dependency.folderName;
});

const rootNodeModules = path.join(process.cwd(), 'node_modules');
const rootNodeModules = path.join(cwd, 'node_modules');
const cacheFolder = path.join(rootNodeModules, `${moduleFolderName}_cache`);
const moduleFolder = path.join(process.cwd(), moduletools.modulesFolder, moduleFolderName);
const moduleFolder = path.join(cwd, moduletools.modulesFolder, moduleFolderName);
const moduleNodeModules = path.join(moduleFolder, 'node_modules');

return systools.moveMultiple(conflictedDependencyFolders, rootNodeModules, moduleNodeModules)
Expand All @@ -159,8 +179,8 @@ function persistExistingConfilctingDependencies(packetsToKeep, moduleFolderName)
return packet.folderName;
});

const rootNodeModules = path.join(process.cwd(), 'node_modules');
const moduleNodeModules = path.join(process.cwd(), moduletools.modulesFolder, moduleFolderName, 'node_modules');
const rootNodeModules = path.join(cwd, 'node_modules');
const moduleNodeModules = path.join(cwd, moduletools.modulesFolder, moduleFolderName, 'node_modules');
return systools.moveMultiple(packetFolderNames, moduleNodeModules, rootNodeModules);
}

Expand Down Expand Up @@ -220,6 +240,42 @@ function installModules(installedModules, moduleInfos, index) {
});
}

function prepareInstallAsDependency() {
if (!installedAsDependency) {
return Promise.resolve();
}

return systools.mkdir(path.join(cwd, 'node_modules'))
.then(() => {
return systools.getFolderNames(path.join(cwd, '..'));
})
.then((folderNames) => {
dependencyInstallLinkedFolders = folderNames;
dependencyInstallLinkedFolders.splice(dependencyInstallLinkedFolders.indexOf(projectFolderName), 1);

return Promise.all(dependencyInstallLinkedFolders.map((folderName) => {
systools.link(path.join(cwd, '..', folderName), path.join(cwd, 'node_modules', folderName))
}));
});
}

function cleanupInstallAsDependency() {
if (!installedAsDependency) {
return Promise.resolve();
}

return systools.deleteMultiple(dependencyInstallLinkedFolders, path.join(cwd, 'node_modules'))
.then(() => {
return systools.getFolderNames(path.join(cwd, 'node_modules'));
})
.then((folderNames) => {
return systools.moveMultiple(folderNames, path.join(cwd, 'node_modules'), path.join(cwd, '..'))
})
.then(() => {
return systools.delete(path.join(cwd, 'node_modules'));
});
}

function run() {
const startTime = Date.now();
if (process.argv[2] && process.argv[2] !== 'isChildProcess') {
Expand All @@ -235,9 +291,15 @@ function run() {
moduletools.setNullTarget('NUL');
}

const pathParts = cwd.split(path.sep);
projectFolderName = pathParts[pathParts.length - 1];

let installedModules;
let moduleInfos;
checkStartConditions()
.then(() => {
return prepareInstallAsDependency();
})
.then(() => {
return Promise.all([
getInstalledModules(),
Expand All @@ -257,12 +319,15 @@ function run() {
}
logIfInRoot(`minstall found ${moduleInfos.length} local ${moduleText} in '${moduletools.modulesFolder}'`);
return Promise.all(moduleInfos.map((moduleInfo) => {
return systools.delete(path.join(process.cwd(), 'node_modules', moduleInfo.folderName));
return systools.delete(path.join(cwd, 'node_modules', moduleInfo.folderName));
}));
})
.then(() => {
return installModules(installedModules, moduleInfos);
})
.then(() => {
return cleanupInstallAsDependency();
})
.then(() => {
logIfInRoot(`\nminstall finished in ${systools.getRuntime(startTime)} :)\n\n`);
})
Expand Down
20 changes: 19 additions & 1 deletion lib/systools.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const systools = {
}));
},

deleteMultiple(names, from) {
return Promise.all(names.map((name) => {
return this.delete(path.join(from, name));
}));
},

link(modulePath, targetPath) {
return new Promise((resolve, reject) => {
fs.symlink(modulePath, targetPath, 'junction', (error) => {
Expand All @@ -56,9 +62,21 @@ const systools = {
});
},

mkdir(location) {
return new Promise((resolve, reject) => {
fs.mkdirs(location, (error) => {
if (error) {
return reject();
}

return resolve();
})
});
},

runCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
exec(command, {maxBuffer: 2097152}, (error, stdout, stderr) => {
if (error !== null) {
console.log('ERROR RUNNING COMMAND', command, error);
return reject(error);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minstall",
"version": "1.3.7",
"version": "1.4.0",
"description": "local module installer",
"main": "lib/minstall.js",
"bin": "lib/minstall.js",
Expand Down

0 comments on commit 65e15ef

Please sign in to comment.