Skip to content

Commit

Permalink
Copy packages into browser_modules
Browse files Browse the repository at this point in the history
Copy required dependencies rather than symlinking them during
installation so that if a package moves after Intern has been installed
it won't be stuck with a broken link.
  • Loading branch information
jason0x43 committed Jun 28, 2016
1 parent 1728a27 commit bcd526c
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions support/fixdeps.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
/*jshint node:true */
var fs = require('fs');
var path = require('path');
var expected = path.join(__dirname, '..', 'browser_modules');
var browserModules = path.join(__dirname, '..', 'browser_modules');

if (!fs.existsSync(expected)) {
fs.mkdirSync(expected);
function getPackageVersion(packageJsonPath) {
var packageJson = fs.readFileSync(packageJsonPath, { encoding: 'utf8' });
return JSON.parse(packageJson).version;
}

// AMD-loaded dependencies need to exist in a known location, so they're symlinked from the path resolved by the node
// loader into browser_modules
if (!fs.existsSync(browserModules)) {
fs.mkdirSync(browserModules);
}

// AMD-loaded dependencies need to exist in a known location, but npm's deduplication process can make final package
// locations unpredictable, so copy the currently installed versions of required packages into browser_modules.
[ 'dojo', 'chai', 'diff' ].forEach(function (dependency) {
var expectedPath = path.join(expected, dependency);
var packageJson = require.resolve(path.join(dependency, 'package.json'));
var actualPath = path.dirname(packageJson);
var installedPath = path.dirname(packageJson);
var expectedPath = path.join(browserModules, dependency);
var installedVersion = getPackageVersion(packageJson);
var existingVersion;

// Check for presence of package; if it's there but older than the node-installed package, replace it
try {
if (fs.statSync(expectedPath).isDirectory()) {
var existingPackageJson = fs.readFileSync(path.join(expectedPath, 'package.json'), { encoding: 'utf8' });
}
existingVersion = getPackageVersion(path.join(expectedPath, 'package.json'));
}
catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

if (actualPath.indexOf(expectedPath) !== 0) {
try {
fs.symlinkSync(path.relative(path.dirname(expectedPath), actualPath), expectedPath, 'junction');
}
catch (error) {
console.warn('Symlinking %s to %s failed with %s. Copying instead...', actualPath, expectedPath,
error.code);
copy(actualPath, expectedPath);
}
// The package in browser_modules is older than the installed version; delete it
if (existingVersion && existingVersion !== installedVersion) {
unlink(expectedPath);
existingVersion = null;
}

// The package isn't in browser_modules, so copy the installed one
if (!existingVersion) {
copy(installedPath, expectedPath);
}
});

Expand Down Expand Up @@ -68,3 +72,26 @@ function copy(source, target) {
fs.writeFileSync(target, fs.readFileSync(source), { mode: stats.mode });
}
}

function unlink(target) {
try {
var stats = fs.statSync(target);
}
catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}

return;
}

if (stats.isDirectory()) {
fs.readdirSync(target).forEach(function (filename) {
unlink(path.join(target, filename));
});
fs.rmdirSync(target);
}
else if (stats.isFile()) {
fs.unlinkSync(target);
}
}

0 comments on commit bcd526c

Please sign in to comment.