From df181558cb59d05202b3c12ba431c8ad855ebdb2 Mon Sep 17 00:00:00 2001 From: MURAKAMI Masahiko Date: Sun, 12 Aug 2018 11:00:09 +0900 Subject: [PATCH] Add Support TypeScript close #12 --- README.md | 7 ++- __tests__/app.js | 13 +++++- __tests__/babel.js | 39 ++++++++++++++++ __tests__/typescript.js | 39 ++++++++++++++++ generators/app/index.js | 19 +++++++- generators/typescript/index.js | 38 +++++++++++++++ generators/typescript/templates/README.md | 26 +++++++++++ generators/typescript/templates/_clasp.json | 4 ++ generators/typescript/templates/_gitignore | 2 + generators/typescript/templates/_package.json | 46 +++++++++++++++++++ generators/typescript/templates/src/hello.ts | 4 ++ generators/typescript/templates/src/index.ts | 2 + generators/typescript/templates/src/shim.d.ts | 1 + generators/typescript/templates/tsconfig.json | 17 +++++++ 14 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 __tests__/babel.js create mode 100644 __tests__/typescript.js create mode 100644 generators/typescript/index.js create mode 100644 generators/typescript/templates/README.md create mode 100644 generators/typescript/templates/_clasp.json create mode 100644 generators/typescript/templates/_gitignore create mode 100644 generators/typescript/templates/_package.json create mode 100644 generators/typescript/templates/src/hello.ts create mode 100644 generators/typescript/templates/src/index.ts create mode 100644 generators/typescript/templates/src/shim.d.ts create mode 100644 generators/typescript/templates/tsconfig.json diff --git a/README.md b/README.md index 2bc1577..00b9ae4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # generator-gas [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url] -> +> Generator for Google Apps Script Project. + +You can choose transpiler to local development which of the following: + +- TypeScript +- Babel ## Installation diff --git a/__tests__/app.js b/__tests__/app.js index ccefe06..4eb5e28 100644 --- a/__tests__/app.js +++ b/__tests__/app.js @@ -6,8 +6,12 @@ const helpers = require('yeoman-test'); describe('generator-gas:app', () => { beforeAll(() => { return helpers - .run(path.join(__dirname, '../generators/app')) - .withPrompts({someAnswer: true}); + .run(path.join(__dirname, '../generators/app')) + .withPrompts({ + appName: 'test-app', + githubUsername: 'fossamagna', + transpiler: 'babel' + }); }); it('creates files', () => { @@ -21,5 +25,10 @@ describe('generator-gas:app', () => { 'src/index.js', 'src/hello.js' ]); + + // assert.jsonFileContent('package.json', { + // name: 'test-app', + // repository: 'fossamagna/test-app' + // }); }); }); diff --git a/__tests__/babel.js b/__tests__/babel.js new file mode 100644 index 0000000..4b1db3f --- /dev/null +++ b/__tests__/babel.js @@ -0,0 +1,39 @@ +'use strict'; +const path = require('path'); +const assert = require('yeoman-assert'); +const helpers = require('yeoman-test'); + +describe('generator-gas:babel', () => { + beforeAll(() => { + return helpers + .run(path.join(__dirname, '../generators/babel')) + .withOptions({ + appName: 'test-app', + githubUsername: 'fossamagna', + name: 'Full Name', + email: 'fossamagna2@gmail.com' + }); + }); + + it('creates files', () => { + assert.file([ + '.babelrc', + '.gitignore', + 'package.json', + '.clasp.json', + '.eslintrc.json', + 'README.md', + 'src/index.js', + 'src/hello.js' + ]); + + assert.jsonFileContent('package.json', { + name: 'test-app', + repository: 'fossamagna/test-app', + author: { + name: 'Full Name', + email: 'fossamagna2@gmail.com' + } + }); + }); +}); diff --git a/__tests__/typescript.js b/__tests__/typescript.js new file mode 100644 index 0000000..36b9584 --- /dev/null +++ b/__tests__/typescript.js @@ -0,0 +1,39 @@ +'use strict'; +const path = require('path'); +const assert = require('yeoman-assert'); +const helpers = require('yeoman-test'); + +describe('generator-gas:typescript', () => { + beforeAll(() => { + return helpers + .run(path.join(__dirname, '../generators/typescript')) + .withOptions({ + appName: 'test-app', + githubUsername: 'fossamagna', + name: 'Full Name', + email: 'fossamagna2@gmail.com' + }); + }); + + it('creates files', () => { + assert.file([ + '.gitignore', + 'package.json', + '.clasp.json', + 'README.md', + 'src/index.ts', + 'src/hello.ts', + 'src/shim.d.ts', + 'tsconfig.json', + ]); + + assert.jsonFileContent('package.json', { + name: 'test-app', + repository: 'fossamagna/test-app', + author: { + name: 'Full Name', + email: 'fossamagna2@gmail.com' + } + }); + }); +}); diff --git a/generators/app/index.js b/generators/app/index.js index a3c59bb..89b6cbd 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -26,6 +26,22 @@ module.exports = class extends Generator { validate: val => { return val.length > 0 ? true : 'You have to provide a username'; } + }, + { + type: 'list', + name: 'transpiler', + message: 'What do the transpiler use in your app?', + default: 'typescript', + choices: [ + { + name: 'TypeScript', + value: 'typescript' + }, + { + name: 'Babel', + value: 'babel' + } + ] } ]; @@ -36,7 +52,8 @@ module.exports = class extends Generator { } configuring() { - this.composeWith(require.resolve('../babel'), { + const transpiler = this.props.transpiler; + this.composeWith(require.resolve(`../${transpiler}`), { appName: this.props.appName, githubUsername: this.props.githubUsername, name: this.user.git.name(), diff --git a/generators/typescript/index.js b/generators/typescript/index.js new file mode 100644 index 0000000..da8f33e --- /dev/null +++ b/generators/typescript/index.js @@ -0,0 +1,38 @@ +'use strict'; +const Generator = require('yeoman-generator'); + +module.exports = class extends Generator { + + constructor(args, opts) { + super(args, opts); + } + + writing() { + this.fs.copyTpl( + this.templatePath('**'), + this.destinationPath(), + { + appName: this.options.appName, + githubUsername: this.options.githubUsername, + name: this.options.name, + email: this.options.email + } + ); + this.fs.move( + this.destinationPath('_package.json'), + this.destinationPath('package.json') + ); + this.fs.move( + this.destinationPath('_gitignore'), + this.destinationPath('.gitignore') + ); + this.fs.move( + this.destinationPath('_clasp.json'), + this.destinationPath('.clasp.json') + ); + } + + install() { + this.installDependencies({bower: false}); + } +}; diff --git a/generators/typescript/templates/README.md b/generators/typescript/templates/README.md new file mode 100644 index 0000000..41b6171 --- /dev/null +++ b/generators/typescript/templates/README.md @@ -0,0 +1,26 @@ +# <%= appName %> + +Google Apps Script Application with [Browserify](http://browserify.org) + [TypeScript](https://www.typescriptlang.org/). + +# For Local Development + +For Apps Script local development, You need to be enable Apps Script API: https://script.google.com/home/usersettings. This project will be use [clasp](https://github.com/google/clasp) to push Apps Script Project. + +.clasp.json: +```json +{ + "scriptId": "", + "rootDir": "dist" +} +``` + +Please see [clasp](https://github.com/google/clasp) and [Command Line Interface using clasp](https://developers.google.com/apps-script/guides/clasp) for details. + +# Deploy + +```sh +$ npm run deploy +``` + +* Server JavaScript files will be compiled by Browserify and [gasify](https://www.npmjs.com/package/gasify). +* Sync compiled `Code.js` to Google Apps Script Project in Google Drive by clasp. diff --git a/generators/typescript/templates/_clasp.json b/generators/typescript/templates/_clasp.json new file mode 100644 index 0000000..9ac5325 --- /dev/null +++ b/generators/typescript/templates/_clasp.json @@ -0,0 +1,4 @@ +{ + "scriptId": "", + "rootDir": "dist" +} diff --git a/generators/typescript/templates/_gitignore b/generators/typescript/templates/_gitignore new file mode 100644 index 0000000..f06235c --- /dev/null +++ b/generators/typescript/templates/_gitignore @@ -0,0 +1,2 @@ +node_modules +dist diff --git a/generators/typescript/templates/_package.json b/generators/typescript/templates/_package.json new file mode 100644 index 0000000..b0648a2 --- /dev/null +++ b/generators/typescript/templates/_package.json @@ -0,0 +1,46 @@ +{ + "name": "<%= appName %>", + "version": "1.0.0", + "description": "Google Apps Script Application with Browserify + TypeScript", + "main": "index.js", + "scripts": { + "test": "eslint src", + "clean": "rimraf dist", + "initialize": "mkdirp dist", + "watch": "npm-run-all initialize -p watch:gs", + "prefetch:manifest": "npm-run-all initialize", + "fetch:manifest": "clasp pull", + "postfetch:manifest": "cpx ./dist/appsscript.json ./src/appsscript.json", + "prebuild:gs": "cpx ./src/appsscript.json ./dist/appsscript.json", + "build:gs": + "browserify ./src/index.ts -p tsify -p gasify -o ./dist/Code.js", + "watch:gs": "watchify ./src/index.ts -p tsify -p gasify -o ./dist/Code.js", + "build": "npm-run-all clean initialize build:gs", + "upload": "claps push", + "deploy": "npm-run-all build upload" + }, + "keywords": ["google", "apps-script"], + "author": { + "name": "<%= name %>", + "email": "<%= email %>" + }, + "repository": "<%= githubUsername %>/<%= appName %>", + "license": "MIT", + "devDependencies": { + "@types/google-apps-script": "0.0.27", + "@google/clasp": "1.4.0", + "browserify": "16.2.2", + "browserify-shim": "3.8.13", + "cpx": "1.5.0", + "eslint": "5.2.0", + "gasify": "0.1.2", + "mkdirp": "0.5.1", + "npm-run-all": "4.1.3", + "onchange": "4.1.0", + "rimraf": "2.4.4", + "tsify": "4.0.0", + "typescript": "3.0.1", + "watch": "1.0.2", + "watchify": "3.6.1" + } +} diff --git a/generators/typescript/templates/src/hello.ts b/generators/typescript/templates/src/hello.ts new file mode 100644 index 0000000..3829bb6 --- /dev/null +++ b/generators/typescript/templates/src/hello.ts @@ -0,0 +1,4 @@ +export default function helloWorldMessage() { + Logger.log('hello world'); +} + \ No newline at end of file diff --git a/generators/typescript/templates/src/index.ts b/generators/typescript/templates/src/index.ts new file mode 100644 index 0000000..4c5f4f7 --- /dev/null +++ b/generators/typescript/templates/src/index.ts @@ -0,0 +1,2 @@ +import hello from './hello'; +global.hello = hello; diff --git a/generators/typescript/templates/src/shim.d.ts b/generators/typescript/templates/src/shim.d.ts new file mode 100644 index 0000000..53e0ca5 --- /dev/null +++ b/generators/typescript/templates/src/shim.d.ts @@ -0,0 +1 @@ +declare var global: any; \ No newline at end of file diff --git a/generators/typescript/templates/tsconfig.json b/generators/typescript/templates/tsconfig.json new file mode 100644 index 0000000..eb90b5a --- /dev/null +++ b/generators/typescript/templates/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "outDir": "./dist", + "target": "es3", + "module": "commonjs", + "declaration": true, + "sourceMap": true, + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "compileOnSave": true, + "rootDir": "src", + "include": ["./src/**/*"] +}