Skip to content

Commit

Permalink
feat: add template for typescript (#1319)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored and malept committed Dec 4, 2019
1 parent 371a068 commit cece7da
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"sourceMaps": "inline",
"ignore": [
"./tmpl"
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"source-map-support"
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ dist
node_modules
*.d.ts
packages/*/*/index.ts
tmpl
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ yarn-error.log
packages/.old
**/tsconfig.json
!/tsconfig.json
!packages/template/*/tmpl/tsconfig.json
**/tslint.json
!/tslint.json
!packages/template/*/tmpl/tslint.json
docs
doc
.vscode
Expand Down
1 change: 1 addition & 0 deletions packages/api/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@electron-forge/publisher-base": "6.0.0-beta.46",
"@electron-forge/shared-types": "6.0.0-beta.46",
"@electron-forge/template-webpack": "6.0.0-beta.46",
"@electron-forge/template-typescript": "6.0.0-beta.46",
"@electron/get": "^1.6.0",
"colors": "^1.4.0",
"cross-spawn-promise": "^0.10.1",
Expand Down
35 changes: 33 additions & 2 deletions packages/api/core/test/slow/api_spec_slow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => {
});
});

describe('init (with built-in templater)', () => {
describe('init (with webpack templater)', () => {
before(ensureTestDirIsNonexistent);

it('should succeed in initializing', async () => {
it('should succeed in initializing the webpack template', async () => {
await forge.init({
dir,
template: 'webpack',
Expand Down Expand Up @@ -165,6 +165,37 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => {
});
});

describe('init (with typescript templater)', () => {
before(ensureTestDirIsNonexistent);

it('should succeed in initializing the typescript template', async () => {
await forge.init({
dir,
template: 'typescript',
});
});

it('should copy the appropriate template files', async () => {
const expectedFiles = [
'tsconfig.json',
'tslint.json',
];
for (const filename of expectedFiles) {
await expectProjectPathExists(filename, 'file');
}
});

it('should convert the main process file to typescript', async () => {
await expectProjectPathNotExists(path.join('src', 'index.js'), 'file');
await expectProjectPathExists(path.join('src', 'index.ts'), 'file');
expect((await fs.readFile(path.join(dir, 'src', 'index.ts'))).toString()).to.match(/Electron.BrowserWindow/);
});

after(async () => {
await fs.remove(dir);
});
});

describe('init (with a nonexistent templater)', () => {
before(ensureTestDirIsNonexistent);

Expand Down
21 changes: 21 additions & 0 deletions packages/template/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@electron-forge/template-typescript",
"version": "6.0.0-beta.46",
"description": "Typescript template for Electron Forge, gets you started with TypeScript really quickly",
"repository": "https://github.com/electron-userland/electron-forge",
"author": "Shelley Vohr <shelley.vohr@gmail.com>",
"license": "MIT",
"main": "dist/TypeScriptTemplate.js",
"typings": "dist/TypeScriptTemplate.d.ts",
"scripts": {
"test": "echo No Tests"
},
"engines": {
"node": ">= 8.0"
},
"dependencies": {
"@electron-forge/async-ora": "6.0.0-beta.46",
"@electron-forge/shared-types": "6.0.0-beta.46",
"fs-extra": "^8.1.0"
}
}
46 changes: 46 additions & 0 deletions packages/template/typescript/src/TypeScriptTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ForgeTemplate } from '@electron-forge/shared-types';
import { asyncOra } from '@electron-forge/async-ora';

import fs from 'fs-extra';
import path from 'path';

const copyTemplateFile = async (destDir: string, basename: string) => {
const templateDir = path.resolve(__dirname, '..', 'tmpl');
await fs.copy(path.join(templateDir, basename), path.resolve(destDir, basename));
};

class TypeScriptTemplate implements ForgeTemplate {
public devDependencies = [
'typescript@^3.7.0',
'tslint@^5.20.0',
];

public initializeTemplate = async (directory: string) => {
await asyncOra('Setting up Forge configuration', async () => {
const packageJSONPath = path.resolve(directory, 'package.json');
const packageJSON = await fs.readJson(packageJSONPath);

// Configure scripts for TS template
packageJSON.scripts.lint = 'tslint -c tslint.json -p tsconfig.json';
packageJSON.scripts.start = 'tsc && electron-forge start -p dist';

await fs.writeJson(packageJSONPath, packageJSON, { spaces: 2 });
});

await asyncOra('Setting up TypeScript configuration', async () => {
const filePath = (fileName: string) => path.join(directory, 'src', fileName);

// Copy tsconfig with a small set of presets
await copyTemplateFile(directory, 'tsconfig.json');

// Copy tslint config with recommended settings
await copyTemplateFile(directory, 'tslint.json');

// Remove index.js and replace with index.ts
await fs.remove(filePath('index.js'));
await copyTemplateFile(path.join(directory, 'src'), 'index.ts');
});
}
}

export default new TypeScriptTemplate();
58 changes: 58 additions & 0 deletions packages/template/typescript/tmpl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { app, BrowserWindow } from 'electron';
import * as path from 'path';

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow: Electron.BrowserWindow;

const createWindow = () => {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
});

// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, "../src/index.html"));

// Open the DevTools.
mainWindow.webContents.openDevTools();

// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
18 changes: 18 additions & 0 deletions packages/template/typescript/tmpl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"allowJs": true,
"module": "commonjs",
"noImplicitAny": true,
"sourceMap": true,
"baseUrl": ".",
"outDir": "dist",
"moduleResolution": "node",
"resolveJsonModule": true,
"paths": {
"*": ["node_modules/*"]
}
},
"include": [
"src/**/*"
]
}
9 changes: 9 additions & 0 deletions packages/template/typescript/tmpl/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:latest"
],
"jsRules": {},
"rules": {},
"rulesDirectory": []
}
9 changes: 5 additions & 4 deletions tsconfig.packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"compilerOptions": {
"rootDir": "packages"
},
"exclude": [
"exclude": [
"dist",
"node_modules",
"ci",
"tools",
"**/dist",
"**/node_modules",
"**/test",
]
}
"**/test",
"**/tmpl"
]
}

0 comments on commit cece7da

Please sign in to comment.