Skip to content

Instantly share code, notes, and snippets.

@blink1073
Last active March 16, 2017 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blink1073/598c3512d7018e02ebbd6c321af9a01b to your computer and use it in GitHub Desktop.
Save blink1073/598c3512d7018e02ebbd6c321af9a01b to your computer and use it in GitHub Desktop.
JupyterLab Big Split Utilities
if [ "$#" -ne 1 ]; then
echo "Specify the target"
fi
# copy default files
cp tsconfig.json $1
cp package.json $1
cd $1
# make source and optionally style directories
mkdir src
git mv *.ts src
PATTERN=(./*.css)
if [ -f ${PATTERN[0]} ]; then
mkdir style
git mv *.css style
fi
cd ..
# Handle the dependencies
node handle_dependencies.js $1
cd $1
# Replace the relative imports
find . -name "*.ts" -exec sed -i "" -e "s/from '\.\./from '@jupyterlab/g" {} +
git add .
# Open the package.json for editing
subl package.json
cd ..
/**
* Populate the package.json file with the required dependencies from the TypeScript source files.
*/
var readFileSync = require("fs").readFileSync;
var writeFileSync = require("fs").writeFileSync;
var ts = require("typescript");
var glob = require("glob");
var childProcess = require('child_process');
var path = require('path');
/**
* Extract the module imports from a TypeScript source file.
*/
function getImports(sourceFile) {
var imports = [];
handleNode(sourceFile);
function handleNode(node) {
switch (node.kind) {
case ts.SyntaxKind.ImportDeclaration:
var name = node.moduleSpecifier.getText();
name = name.slice(1, name.length - 1);
imports.push(name);
break;
}
ts.forEachChild(node, handleNode);
}
return imports;
}
var dname = process.argv[2];
/**
* Get all of the TypeScript files in the source directory.
*/
glob(dname + '/src/*.ts', function(er, filenames) {
var imports = [];
var package = require(path.resolve(dname) + '/package.json');
// Set up the package.json file.
package['name'] = '@jupyterlab/' + path.basename(dname);
package['version'] = '0.1.0';
// Extract all of the imports from the TypeScript files.
filenames.forEach(fileName => {
let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true);
imports = imports.concat(getImports(sourceFile));
});
var names = Array.from(new Set(imports)).sort();
// Handle local imports with starting package version.
for (let i = 0; i < names.length; i++) {
if (names[i].slice(0, 2) === '..') {
process.stdout.write(names[i] + '\n');
let name = names[i].split('../').join('');
name = '@jupyterlab/' + name;
package['dependencies'][name] = '^0.1.0';
}
}
// Write to disk now so `npm --save` can work later.
writeFileSync(path.resolve(dname) + '/package.json', JSON.stringify(package, null, 2));
// Add external dependencies.
process.chdir(dname);
for (let i = 0; i < names.length; i++) {
if (names[i] in package['dependencies']) {
continue;
}
if (names[i][0] !== '.') {
process.stdout.write(names[i] + '\n');
var cmd = 'npm install --save ' + names[i] + '@latest';
try {
childProcess.execSync(cmd, { stdio: [0, 1, 2] });
} catch (err) {
process.stdout.write('skipping ' + names[i] + '\n');
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment