Merge pull request #51 from OctoD/ts

chore: Typescript gulp tasks
This commit is contained in:
Mattia Astorino 2017-05-25 11:11:38 +02:00 committed by GitHub
commit 300ca7614f
32 changed files with 440 additions and 294 deletions

3
.gitignore vendored
View file

@ -1,4 +1,7 @@
*.log *.log
*.~lock *.~lock
*.js
**/*.map
*.gulp/**.js
dist/ dist/
node_modules/ node_modules/

5
.gulp/consts/files.ts Normal file
View file

@ -0,0 +1,5 @@
/**
* File charset
* @type {string}
*/
export const CHARSET: string = 'utf-8';

6
.gulp/consts/log.ts Normal file
View file

@ -0,0 +1,6 @@
export const HR: string = '\n———————————————————————————————————————————————————————————————\n';
export const MESSAGE_BUMP_ERROR: string = ' There was an issue bumping version:\n';
export const MESSAGE_BUMP_SUCCESS: string = ' Finished successfully\n';
export const MESSAGE_ICON_ERROR: string = 'There is an error with JSON generated for icons';
export const MESSAGE_GENERATED: string = 'Generated';
export const MESSAGE_THEME_VARIANT_PARSE_ERROR: string = 'Error when parsing json for theme variants';

10
.gulp/consts/paths.ts Normal file
View file

@ -0,0 +1,10 @@
import { IPaths } from '../interfaces/ipaths';
const PATHS: IPaths = {
DIST: './dist',
ICONS: './icons',
SRC: './src',
THEMES: './themes',
};
export default PATHS;

View file

@ -1,9 +0,0 @@
// import the tasks
import './tasks/changelog';
import './tasks/bump';
import './tasks/icons';
import './tasks/themes';
import './tasks/watcher';
// export default script
export default ['build:themes'];

9
.gulp/index.ts Normal file
View file

@ -0,0 +1,9 @@
// export the tasks
export * from './tasks/changelog';
export * from './tasks/bump';
export * from './tasks/icons';
export * from './tasks/themes';
export * from './tasks/watcher';
// export default script
export default ['build:themes'];

14
.gulp/interfaces/iicon.ts Normal file
View file

@ -0,0 +1,14 @@
export interface IIcon {
/**
* If set to true, the icon is marked as last
* @type {boolean}
* @memberof IIcon
*/
last: boolean;
/**
* Icon's name
* @type {string}
* @memberof IIcon
*/
name: string;
}

View file

@ -0,0 +1,26 @@
export interface IPaths {
/**
* Dist dir
* @type {string}
* @memberof IPaths
*/
DIST: string;
/**
* Icons dir
* @type {string}
* @memberof IPaths
*/
ICONS: string;
/**
* Src dir
* @type {string}
* @memberof IPaths
*/
SRC: string;
/**
* Themes dir
* @type {string}
* @memberof IPaths
*/
THEMES: string;
}

View file

@ -0,0 +1,3 @@
export interface IPlainObject {
[index: string]: string;
}

View file

@ -0,0 +1,42 @@
export interface IThemeVariant {
id: string;
name: string;
scheme: {
background: string;
base: {
black: string;
blue: string;
brown: string;
cyan: string;
green: string;
orange: string;
paleblue: string;
pink: string;
purple: string;
red: string;
violet: string;
white: string;
yellow: string;
}
caret: string;
comments: string;
findHighlight: string;
focusBorder: string;
foreground: string;
guides: string;
inputBackground: string;
inputBorder: string;
inputForeground: string;
invisibles: string;
lineHighlight: string;
lineNumbers: string;
listHoverForeground: string;
scrollbars: string;
scrollbarsHover: string;
selection: string;
shadow: string;
sidebarForeground: string;
statusbarForeground: string;
}
type: string;
}

View file

@ -1,11 +0,0 @@
import infos from '../package.json';
const today = new Date()
, paths = {
'icons': './icons',
'themes': './themes',
'src': './src',
'dist': './dist'
};
export default paths;

View file

@ -1,37 +0,0 @@
'use strict';
/*
* > Bump
*/
import Gulp from 'gulp';
import runSequence from 'run-sequence';
import gutil from 'gulp-util';
import yrgv from 'yargs';
import bump from 'gulp-bump';
import Gulpif from 'gulp-if';
var argv = yrgv.argv;
Gulp.task('bump', (cb) => {
runSequence(
'bump-pkg-version',
(error) => {
if (error) {
console.log(gutil.colors.magenta.bold('[bump]'), gutil.colors.red.bold(' There was an issue bumping version:\n'), error.message);
} else {
console.log(gutil.colors.magenta.bold('\n[bump]'), gutil.colors.green.bold(' Finished successfully\n'));
}
cb(error);
}
);
});
Gulp.task('bump-pkg-version', () => {
return Gulp.src(['./package.json'])
.pipe(Gulpif((Object.keys(argv).length === 2), bump()))
.pipe(Gulpif(argv.patch, bump()))
.pipe(Gulpif(argv.minor, bump({ type: 'minor' })))
.pipe(Gulpif(argv.major, bump({ type: 'major' })))
.pipe(Gulp.dest('./'));
});

35
.gulp/tasks/bump.ts Normal file
View file

@ -0,0 +1,35 @@
import * as bump from 'gulp-bump';
import * as gulp from 'gulp';
import * as gulpIf from 'gulp-if';
import * as gulpUtil from 'gulp-util';
import * as runSequence from 'run-sequence';
import * as yargs from 'yargs';
import { MESSAGE_BUMP_ERROR, MESSAGE_BUMP_SUCCESS } from "../consts/log";
var argv = yargs.argv;
export var taskBump = gulp.task('bump', (cb) => {
runSequence(
'bump-pkg-version',
error => {
if (error) {
console.log(gulpUtil.colors.magenta.bold('[bump]'), gulpUtil.colors.red.bold(MESSAGE_BUMP_ERROR), error);
} else {
console.log(gulpUtil.colors.magenta.bold('\n[bump]'), gulpUtil.colors.green.bold(MESSAGE_BUMP_SUCCESS));
}
cb(error);
}
);
});
export var taskVersioning = gulp.task('bump-pkg-version', () => {
return gulp.src(['./package.json'])
.pipe(gulpIf((Object.keys(argv).length === 2), bump()))
.pipe(gulpIf(argv.patch, bump()))
.pipe(gulpIf(argv.minor, bump({ type: 'minor' })))
.pipe(gulpIf(argv.major, bump({ type: 'major' })))
.pipe(gulp.dest('./'));
});
export default { taskBump, taskVersioning };

View file

@ -1,18 +0,0 @@
'use strict';
/*
* > Changelog
*/
import Gulp from 'gulp';
import conventionalChangelog from 'gulp-conventional-changelog';
Gulp.task('changelog', () => {
return Gulp.src('CHANGELOG.md')
.pipe(conventionalChangelog({
preset: 'angular',
releaseCount: 0
}))
.pipe(Gulp.dest('./'));
});

15
.gulp/tasks/changelog.ts Normal file
View file

@ -0,0 +1,15 @@
/*
* > Changelog
*/
import * as gulp from 'gulp';
import * as gulpConventionalChangelog from 'gulp-conventional-changelog';
export var task = gulp.task('changelog', () => {
return gulp.src('CHANGELOG.md')
.pipe(gulpConventionalChangelog({
preset: 'angular',
releaseCount: 0
}))
.pipe(gulp.dest('./'));
});

View file

@ -1,55 +0,0 @@
'use strict';
/*
* > Build Icons
*/
import fs from 'fs';
import Gulp from 'gulp';
import Mustache from 'mustache';
import gutil from 'gulp-util';
import Paths from '../paths';
Gulp.task('build:icons', cb => {
const partials = fs.readdirSync(`${Paths.src}/icons/partials`);
const partialData = {};
const files = fs.readdirSync(`${Paths.src}/icons/svgs`);
const icons = files.map(file => ({ name: file.split('.')[0], last: false }));
icons[icons.length - 1].last = true;
partials.forEach(partial => {
partialData[partial.split('.')[0]] = fs.readFileSync(
`${Paths.src}/icons/partials/${partial}`,
'utf-8'
);
});
let contents = Mustache.render(
fs.readFileSync(`${Paths.src}/icons/icons-theme.json`, 'utf-8'),
{ icons },
partialData
);
try {
contents = JSON.stringify(JSON.parse(contents), null, 2);
} catch (err) {
gutil.log(
gutil.colors.red('There is an error with JSON generated for icons'),
err
);
cb(err);
return;
}
const path = './themes/.material-theme-icons.tmp';
fs.writeFileSync(path, contents, 'utf-8');
gutil.log(
gutil.colors.gray('\n———————————————————————————————————————————————————————————————\n')
);
gutil.log('Generated', gutil.colors.green(path));
gutil.log(
gutil.colors.gray('\n———————————————————————————————————————————————————————————————\n')
);
cb();
});

65
.gulp/tasks/icons.ts Normal file
View file

@ -0,0 +1,65 @@
import * as fs from 'fs';
import * as gulp from 'gulp';
import * as gutil from 'gulp-util';
import * as mustache from 'mustache';
import * as path from 'path';
import { HR, MESSAGE_GENERATED, MESSAGE_ICON_ERROR } from './../consts/log';
import { CHARSET } from '../consts/files';
import { IIcon } from './../interfaces/iicon';
import { IPlainObject } from '../interfaces/iplain-object';
import paths from '../consts/paths';
/**
* Returns an object implementing the IIcon interface
* @param {string} fileName
* @returns {IIcon}
*/
function iconFactory(fileName: string): IIcon {
let name: string = path.basename(fileName, path.extname(fileName));
let last: boolean = false;
return { name, last } as IIcon;
}
/**
* > Build Icons
* @returns {gulp.Gulp}
*/
export default gulp.task('build:icons', cb => {
let contents: string;
let fileNames: string[] = fs.readdirSync(path.join(paths.SRC, `./icons/svgs`));
let icons: IIcon[] = fileNames.map(fileName => iconFactory(fileName));
let partials: string[] = fs.readdirSync(path.join(paths.SRC, `./icons/partials`));
let partialsData: IPlainObject = {};
let pathTemp: string = './themes/.material-theme-icons.tmp';
icons[icons.length - 1].last = true;
partials.forEach(partial => {
partialsData[path.basename(partial, path.extname(partial))] = fs.readFileSync(path.join(paths.SRC, `./icons/partials`, `./${partial}`), CHARSET);
});
contents = mustache.render(
fs.readFileSync(path.join(paths.SRC, `./icons/icons-theme.json`), CHARSET)
, { icons }
, partialsData
);
try {
contents = JSON.stringify(JSON.parse(contents), null, 2);
} catch (error) {
gutil.log(gutil.colors.red(MESSAGE_ICON_ERROR), error);
cb(error);
return;
}
fs.writeFileSync(pathTemp, contents, CHARSET);
gutil.log(gutil.colors.gray(HR));
gutil.log(MESSAGE_GENERATED, gutil.colors.green(pathTemp));
gutil.log(gutil.colors.gray(HR));
cb();
});

View file

@ -1,65 +0,0 @@
'use strict';
/*
* > Build Themes
*/
import fs from 'fs';
import Gulp from 'gulp';
import gutil from 'gulp-util';
import Mustache from 'mustache';
import YAML from 'yamljs';
import Paths from '../paths';
const themeCommons = require('../../src/themes/settings/commons.json');
const themeVariants = [];
const themeTemplateFile = fs.readFileSync(
`${Paths.src}/themes/theme-template-color-theme.json`,
'utf-8'
);
const files = fs.readdirSync(`${Paths.src}/themes/settings/specific`);
// build theme variants for later use in templating
files.forEach(file => {
const name = file.split('.')[0];
const filepath = `${Paths.src}/themes/settings/specific/${file}`;
const contents = fs.readFileSync(filepath, 'utf-8');
try {
themeVariants.push(JSON.parse(contents));
} catch (err) {
gutil.log('Error when parsing json for theme variants', err);
}
});
Gulp.task('build:themes', cb => {
gutil.log(
gutil.colors.gray('\n———————————————————————————————————————————————————————————————\n')
);
themeVariants.forEach(variant => {
const templateData = {
'commons': themeCommons,
variant
};
const templateJson = JSON.parse(
Mustache.render(themeTemplateFile, templateData)
);
const path = `${Paths.themes}/${variant.name}.json`;
fs.writeFileSync(
path,
JSON.stringify(templateJson, null, 2),
'utf-8'
);
gutil.log('Generate', gutil.colors.green(path));
});
gutil.log(
gutil.colors.gray('\n———————————————————————————————————————————————————————————————\n')
);
});

51
.gulp/tasks/themes.ts Normal file
View file

@ -0,0 +1,51 @@
import * as fs from 'fs';
import * as gulp from 'gulp';
import * as gulpUtil from 'gulp-util';
import * as mustache from 'mustache';
import * as path from 'path';
import { HR, MESSAGE_GENERATED, MESSAGE_THEME_VARIANT_PARSE_ERROR } from './../consts/log';
import { CHARSET } from '../consts/files';
import { IThemeVariant } from './../interfaces/itheme-variant';
import paths from '../consts/paths';
let commons = require('../../src/themes/settings/commons.json');
let themeTemplateFileContent: string = fs.readFileSync(path.join(paths.SRC, `/themes/theme-template-color-theme.json`), CHARSET);
let themeVariants: IThemeVariant[] = [];
let fileNames: string[] = fs.readdirSync(path.join(paths.SRC, `./themes/settings/specific`));
// build theme variants for later use in templating
fileNames.forEach(fileName => {
let filePath: string = path.join(paths.SRC, `./themes/settings/specific`, `./${fileName}`);
let contents: string = fs.readFileSync(filePath, CHARSET);
try {
themeVariants.push(JSON.parse(contents));
} catch (error) {
gulpUtil.log(MESSAGE_THEME_VARIANT_PARSE_ERROR, error);
}
});
/**
* Themes task
* Builds Themes
*/
export default gulp.task('build:themes', () => {
gulpUtil.log(gulpUtil.colors.gray(HR));
themeVariants.forEach(variant => {
let filePath = path.join(paths.THEMES, `./${variant.name}.json`);
let templateData = { commons, variant };
let templateJSON: any = JSON.parse(mustache.render(themeTemplateFileContent, templateData));
let templateJSONStringified: string = JSON.stringify(templateJSON, null, 2);
fs.writeFileSync(filePath, templateJSONStringified, CHARSET);
gulpUtil.log(MESSAGE_GENERATED, gulpUtil.colors.green(filePath));
});
gulpUtil.log(gulpUtil.colors.gray(HR));
});

View file

@ -1,12 +0,0 @@
'use strict';
/*
* > Watcher
*/
import Gulp from 'gulp';
import Paths from '../paths';
Gulp.task('watch', () => {
Gulp.watch(`${Paths.src}/themes/**/*.json`, ['build:themes']);
});

12
.gulp/tasks/watcher.ts Normal file
View file

@ -0,0 +1,12 @@
import * as gulp from "gulp";
import * as path from "path";
import Paths from "../consts/paths";
/*
* > Watcher
* Watches files and build the themes
*/
export default gulp.task('watch', () => {
gulp.watch(path.join(Paths.SRC, `./themes/**/*.json`), ['build:themes']);
});

View file

@ -10,7 +10,8 @@
"node_modules": true, "node_modules": true,
"**/*.js": { "**/*.js": {
"when": "$(basename).ts" "when": "$(basename).ts"
} },
"**/*.js.map": true
}, },
"files.associations": { "files.associations": {
"*.template": "json" "*.template": "json"

98
.vscode/tasks.json vendored
View file

@ -7,22 +7,88 @@
// ${cwd}: the current working directory of the spawned process // ${cwd}: the current working directory of the spawned process
// A task runner that calls a custom npm script that compiles the extension. // A task runner that calls a custom npm script that compiles the extension.
{ {
"version": "0.1.0", "version": "2.0.0",
// we want to run npm // tasks list (build, build-theme, semver etc)
"command": "npm", "tasks": [
// the command is a shell script {
"isShellCommand": true,
// show the output window only if unrecognized errors occur.
"showOutput": "silent",
// we run the custom script "compile" as defined in package.json
"args": [ "args": [
"run", "run"
"compile", , "build"
"--loglevel",
"silent"
], ],
// The tsc compiler is started in watching mode "isBuildCommand": true,
"isWatching": true, "command": "npm",
// use the standard tsc in watch mode problem matcher to find compile problems in the output. "dependsOn": "tsc",
"problemMatcher": "$tsc-watch" "echoCommand": true,
"isShellCommand": true,
"taskName": "build"
},
{
"linux": {
"args": [
"."
, "-name"
, "\"*.js\""
, "-not"
, "-path"
, "\"./node_modules/*\""
, "-not"
, "-path"
, "\"./src/*\""
, "-not"
, "-path"
, "\"./test/*\""
, "-type"
, "f"
, "-delete"
],
"command": "find",
"isShellCommand": true
},
"osx": {
"args": [
"."
, "-name"
, "\"*.js\""
, "-not"
, "-path"
, "\"./node_modules/*\""
, "-not"
, "-path"
, "\"./src/*\""
, "-not"
, "-path"
, "\"./test/*\""
, "-type"
, "f"
, "-delete"
],
"command": "find",
"isShellCommand": true
},
"command": "",
"echoCommand": true,
"taskName": "clean project"
},
{
"args": [
"run"
, "changelog"
],
"command": "npm",
"echoCommand": true,
"isShellCommand": true,
"taskName": "changelog"
},
{
"args": [
"-p"
, "."
],
"command": "tsc",
"dependsOn": "clean project",
"echoCommand": true,
"isShellCommand": true,
"taskName": "tsc"
}
]
} }

View file

@ -1,9 +0,0 @@
import Gulp from 'gulp';
import GulpStats from 'gulp-stats';
import tasks from './.gulp';
// Use gulp-stats
GulpStats(Gulp);
// set default task
Gulp.task('default', tasks);

9
gulpfile.babel.ts Normal file
View file

@ -0,0 +1,9 @@
import * as Gulp from 'gulp';
import * as GulpStats from 'gulp-stats';
import * as tasks from './.gulp';
// Use gulp-stats
GulpStats(Gulp);
// set default task
Gulp.task('default', tasks.default);

View file

@ -77,6 +77,15 @@
} }
], ],
"devDependencies": { "devDependencies": {
"@types/chalk": "^0.4.31",
"@types/gulp": "^4.0.3",
"@types/gulp-if": "0.0.31",
"@types/gulp-util": "^3.0.31",
"@types/mustache": "^0.8.29",
"@types/run-sequence": "0.0.29",
"@types/through2": "^2.0.33",
"@types/yamljs": "^0.2.30",
"@types/yargs": "^6.6.0",
"babel-core": "^6.24.1", "babel-core": "^6.24.1",
"babel-preset-es2015": "^6.24.1", "babel-preset-es2015": "^6.24.1",
"babel-root-import": "^4.1.8", "babel-root-import": "^4.1.8",

View file

@ -1,58 +0,0 @@
var parseXML = function (data) {
var xml, tmp;
if (!data || typeof data !== "string") {
return null;
}
try {
if (window.DOMParser) { // Standard
tmp = new DOMParser();
xml = tmp.parseFromString(data, "text/xml");
} else { // IE
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
}
} catch (e) {
xml = undefined;
}
if (!xml || !xml.documentElement || xml.getElementsByTagName("parsererror").length) {
jQuery.error("Invalid XML: " + data);
}
return xml;
};
// Bind a function to a context, optionally partially applying any arguments.
var proxy = function (fn, context) {
var tmp, args, proxy;
if (typeof context === "string") {
tmp = fn[context];
context = fn;
fn = tmp;
}
// Quick check to determine if target is callable, in the spec
// this throws a TypeError, but we will just return undefined.
if (!jQuery.isFunction(fn)) {
return undefined;
}
// Simulated bind
args = core_slice.call(arguments, 2);
proxy = function () {
return fn.apply(context || this, args.concat(core_slice.call(arguments)));
};
// Set the guid of unique handler to the same of original handler, so it can be removed
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
return proxy;
};
Sound.play = function () { }
Sound.prototype = { something; }
Sound.prototype.play = function () { }
Sound.prototype.play = myfunc
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.hostname; // => "example.com"

File diff suppressed because one or more lines are too long

22
tsconfig.json Normal file
View file

@ -0,0 +1,22 @@
{
"compilerOptions": {
"lib": [
"es2015"
],
"module": "commonjs",
"allowUnreachableCode": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": true,
"sourceMap": true,
"target": "es6"
},
"include": [
"./**/*",
"./.gulp/**/*"
],
"exclude": [
"node_modules",
"out"
]
}

7
typings/gulp-bump/gulp-bump.d.ts vendored Normal file
View file

@ -0,0 +1,7 @@
declare module "gulp-bump" {
function gulpBump (... args: any[]): any;
namespace gulpBump {}
export = gulpBump;
}

View file

@ -0,0 +1,5 @@
declare module "gulp-conventional-changelog" {
function changelog(... args: any[]): any;
namespace changelog {}
export = changelog;
}

5
typings/gulp-stats/gulp-stats.d.ts vendored Normal file
View file

@ -0,0 +1,5 @@
declare module "gulp-stats" {
function gulpStats(... args: any[]): any;
namespace gulpStats {}
export = gulpStats;
}