All files / tasks link.ts

100% Statements 21/21
100% Branches 2/2
66.67% Functions 4/6
100% Lines 21/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43    42x 42x 42x 42x 42x 42x   42x 1x 1x 1x   1x           1x             1x 1x       42x 2x 2x 2x 2x 1x   2x      
import ITask = grunt.task.ITask;
 
export = function(grunt: IGrunt, packageJson: any) {
	const execa = require('execa');
	const fs = require('fs');
	const path = require('path');
	const process = require('process');
	const pkgDir = require('pkg-dir');
 
	grunt.registerTask('_link', '', function (this: ITask) {
		const done = this.async();
		const packagePath = pkgDir.sync(process.cwd());
		const targetPath = grunt.config('distDirectory');
 
		fs.symlink(
			path.join(packagePath, 'node_modules'),
			path.join(targetPath, 'node_modules'),
			'junction',
			() => {}
		);
		fs.symlink(
			path.join(packagePath, 'package.json'),
			path.join(targetPath, 'package.json'),
			'file',
			() => {}
		);
 
		execa.shell('npm link', { cwd: targetPath })
			.then((result: any) => grunt.log.ok(result.stdout))
			.then(done);
	});
 
	grunt.registerTask('link', 'link', function () {
		const targetPath = grunt.config('distDirectory');
		const dirExists = grunt.file.isDir(targetPath);
		const tasks = ['_link'];
		if (!dirExists) {
			tasks.unshift('dist');
		}
		grunt.task.run(tasks);
	});
};