Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate TypeScript definitions #2279

Merged
merged 9 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ module.exports = function(grunt) {
// Load release task
grunt.loadTasks('tasks/release');

// Load typescript task
grunt.registerTask('typescript', function() {
require('./tasks/typescript/task.js')(grunt);
});

// Load the external libraries used.
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-connect');
Expand Down Expand Up @@ -426,7 +431,7 @@ module.exports = function(grunt) {
'mochaTest'
]);
grunt.registerTask('test:nobuild', ['eslint:test', 'connect', 'mocha']);
grunt.registerTask('yui', ['yuidoc:prod', 'minjson']);
grunt.registerTask('yui', ['yuidoc:prod', 'minjson', 'typescript']);
grunt.registerTask('yui:test', ['yuidoc:prod', 'connect', 'mocha:yui']);
grunt.registerTask('default', ['test']);
grunt.registerTask('saucetest', ['connect', 'saucelabs-mocha']);
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"grunt-release-it": "^1.0.1",
"grunt-saucelabs": "8.6.1",
"grunt-update-json": "^0.2.1",
"html2plaintext": "^1.1.1",
"husky": "^0.14.3",
"jscs-stylish": "^0.3.1",
"karma": "^1.7.1",
Expand All @@ -87,7 +88,8 @@
"mocha": "^3.2.0",
"phantomjs": "^2.1.7",
"prettier": "^1.7.4",
"request": "^2.81.0"
"request": "^2.81.0",
"word-wrap": "^1.2.3"
},
"license": "LGPL-2.1",
"dependencies": {
Expand All @@ -100,14 +102,17 @@
"whatwg-fetch": "^2.0.3"
},
"main": "./lib/p5.js",
"types": "./lib/p5.d.ts",
"files": [
"license.txt",
"lib/p5.min.js",
"lib/p5.js",
"lib/addons/p5.sound.js",
"lib/addons/p5.sound.min.js",
"lib/addons/p5.dom.js",
"lib/addons/p5.dom.min.js"
"lib/addons/p5.dom.min.js",
"lib/p5.d.ts",
"lib/p5.global-mode.d.ts"
],
"description": "[![Build Status](https://travis-ci.org/processing/p5.js.svg?branch=master)](https://travis-ci.org/processing/p5.js) [![npm version](https://badge.fury.io/js/p5.svg)](https://www.npmjs.com/package/p5)",
"bugs": {
Expand Down
7 changes: 4 additions & 3 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ var constants = require('./constants');
* "global" - all properties and methods are attached to the window
* "instance" - all properties and methods are bound to this p5 object
*
* @private
* @class p5
* @constructor
* @param {function} sketch a closure that can set optional preload(),
* setup(), and/or draw() properties on the
* given p5 instance
* @param {HTMLElement|boolean} [node] element to attach canvas to, if a
* @param {HTMLElement|Boolean} [node] element to attach canvas to, if a
* boolean is passed in use it as sync
* @param {boolean} [sync] start synchronously (optional)
* @param {Boolean} [sync] start synchronously (optional)
* @return {p5} a p5 instance
*/
var p5 = function(sketch, node, sync) {
Expand Down
6 changes: 6 additions & 0 deletions tasks/release/release-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ module.exports = function(grunt) {
'./lib/addons/p5.sound.min.js',
'application/javascript'
],
p5ts: ['p5.d.ts', './lib/p5.d.ts', 'text/plain'],
p5globalts: [
'p5.global-mode.d.ts',
'./lib/p5.global-mode.d.ts',
'text/plain'
],
p5zip: ['p5.zip', './p5.zip', 'application/zip']
};

Expand Down
119 changes: 119 additions & 0 deletions tasks/typescript/emit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
var fs = require('fs');
var h2p = require('html2plaintext');
var wrap = require('word-wrap');

function shortenDescription(desc) {
return wrap(h2p(desc).replace(/[\r\n]+/, ''), {
width: 50
});
}

function createEmitter(filename) {
var indentLevel = 0;
var lastText = '';
var currentSourceFile;
var fd = fs.openSync(filename, 'w');

var emit = function(text) {
var indentation = [];
var finalText;

for (var i = 0; i < indentLevel; i++) {
indentation.push(' ');
}

finalText = indentation.join('') + text + '\n';
fs.writeSync(fd, finalText);

lastText = text;
};

emit.description = function(classitem, overload) {
var desc = classitem.description;
if (!desc) {
return;
}

function emitDescription(desc) {
shortenDescription(desc)
.split('\n')
.forEach(function(line) {
emit(' * ' + line);
});
}

emit.sectionBreak();
emit('/**');
emitDescription(desc);
emit(' *');
if (overload) {
var alloverloads = [classitem];
if (classitem.overloads) {
alloverloads = alloverloads.concat(classitem.overloads);
}
if (overload.params) {
overload.params.forEach(function(p) {
var arg = p.name;
var p2;
for (var i = 0; !p2 && i < alloverloads.length; i++) {
if (alloverloads[i].params) {
p2 = alloverloads[i].params.find(
p3 => p3.description && p3.name === arg
);
if (p2) {
if (p.optional) {
arg = '[' + arg + ']';
}
emitDescription('@param ' + arg + ' ' + p2.description);
break;
}
}
}
});
}
if (overload.chainable) {
emitDescription('@chainable');
} else if (overload.return && overload.return.description) {
emitDescription('@return ' + overload.return.description);
}
}
emit(' */');
};

emit.setCurrentSourceFile = function(file) {
if (file !== currentSourceFile) {
currentSourceFile = file;
emit.sectionBreak();
emit('// ' + file);
emit.sectionBreak();
}
};

emit.sectionBreak = function() {
if (lastText !== '' && !/\{$/.test(lastText)) {
emit('');
}
};

emit.getIndentLevel = function() {
return indentLevel;
};

emit.indent = function() {
indentLevel++;
};

emit.dedent = function() {
indentLevel--;
};

emit.close = function() {
fs.closeSync(fd);
};

emit('// This file was auto-generated. Please do not edit it.\n');

return emit;
}

module.exports = createEmitter;
Loading