Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Rewrite the generator to be much more comprehensive
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 16, 2019
1 parent e59650f commit 1aa7226
Show file tree
Hide file tree
Showing 25 changed files with 507 additions and 194 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn.lock
temp
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sudo: false
language: node_js
node_js:
- '6'
- '4'
- '12'
- '10'
- '8'
57 changes: 33 additions & 24 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ const Generator = require('yeoman-generator');
const _s = require('underscore.string');

module.exports = class extends Generator {
init() {
return this.prompt([{
async init() {
const props = await this.prompt([{
name: 'appName',
message: 'What do you want to name your app?',
default: _s.slugify(this.appname),
filter: x => _s.slugify(x)
default: this.appname
}, {
name: 'githubUsername',
message: 'What is your GitHub username?',
Expand All @@ -23,29 +22,39 @@ module.exports = class extends Generator {
store: true,
validate: x => x.length > 0 ? true : 'You have to provide a website URL',
filter: x => normalizeUrl(x)
}]).then(props => {
const tpl = {
appName: props.appName,
classifiedAppName: _s.classify(props.appName),
githubUsername: props.githubUsername,
name: this.user.git.name(),
email: this.user.git.email(),
website: props.website,
humanizedWebsite: humanizeUrl(props.website),
superb: superb()
};
}]);

const mv = (from, to) => {
this.fs.move(this.destinationPath(from), this.destinationPath(to));
};
const template = {
appName: props.appName,
slugifiedAppName: _s.slugify(props.appName),
classifiedAppName: _s.classify(props.appName),
githubUsername: props.githubUsername,
repoUrl: `https://github.com/${props.githubUsername}/${props.slugifiedAppName}`,
name: this.user.git.name(),
email: this.user.git.email(),
website: props.website,
humanizedWebsite: humanizeUrl(props.website),
superb: superb.random()
};

this.fs.copyTpl(`${this.templatePath()}/**`, this.destinationPath(), tpl);
mv('editorconfig', '.editorconfig');
mv('gitattributes', '.gitattributes');
mv('gitignore', '.gitignore');
mv('_package.json', 'package.json');
});
const moveDest = (from, to) => {
this.fs.move(this.destinationPath(from), this.destinationPath(to));
};

const copy = (from, to) => {
this.fs.copy(this.templatePath(from), this.destinationPath(to || from));
};

this.fs.copyTpl(`${this.templatePath()}/*`, this.destinationPath(), template);
copy('build');
copy('static');
moveDest('editorconfig', '.editorconfig');
moveDest('gitattributes', '.gitattributes');
moveDest('gitignore', '.gitignore');
moveDest('travis.yml', '.travis.yml');
moveDest('_package.json', 'package.json');
}

install() {
this.installDependencies({bower: false});
}
Expand Down
104 changes: 73 additions & 31 deletions app/templates/_package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,75 @@
{
"name": "<%= appName %>",
"productName": "<%= classifiedAppName %>",
"version": "0.0.0",
"description": "My <%= superb %> app",
"license": "MIT",
"repository": "<%= githubUsername %>/<%= appName %>",
"author": {
"name": "<%= name %>",
"email": "<%= email %>",
"url": "<%= humanizedWebsite %>"
},
"scripts": {
"test": "xo",
"start": "electron .",
"build": "electron-packager . --out=dist --asar --overwrite --all"
},
"dependencies": {
"electron-debug": "^1.0.0"
},
"devDependencies": {
"devtron": "^1.1.0",
"electron-packager": "^8.0.0",
"electron": "^1.6.6",
"xo": "^0.18.0"
},
"xo": {
"envs": [
"node",
"browser"
]
}
"name": "<%= slugifiedAppName %>",
"productName": "<%= appName %>",
"version": "0.0.0",
"description": "My <%= superb %> app",
"license": "MIT",
"repository": "<%= githubUsername %>/<%= slugifiedAppName %>",
"author": {
"name": "<%= name %>",
"email": "<%= email %>",
"url": "<%= humanizedWebsite %>"
},
"scripts": {
"postinstall": "electron-builder install-app-deps",
"lint": "xo",
"test": "npm run lint",
"start": "electron .",
"pack": "electron-builder --dir",
"dist": "electron-builder --macos --linux --windows",
"release": "np"
},
"dependencies": {
"electron-context-menu": "^0.12.1",
"electron-debug": "^3.0.0",
"electron-store": "^3.3.0",
"electron-unhandled": "^2.2.0",
"electron-updater": "^4.0.6",
"electron-util": "^0.12.0"
},
"devDependencies": {
"electron": "^5.0.4",
"electron-builder": "^20.43.0",
"np": "^5.0.3",
"xo": "^0.24.0"
},
"xo": {
"envs": [
"node",
"browser"
]
},
"np": {
"publish": false,
"releaseDraft": false
},
"build": {
"appId": "com.<%= githubUsername %>.<%= slugifiedAppName %>",
"mac": {
"category": "public.app-category.social-networking",
"darkModeSupport": true
},
"dmg": {
"iconSize": 160,
"contents": [
{
"x": 180,
"y": 170
},
{
"x": 480,
"y": 170,
"type": "link",
"path": "/Applications"
}
]
},
"linux": {
"target": [
"AppImage",
"deb"
],
"category": "Network;Chat"
}
}
}
Binary file added app/templates/build/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/templates/build/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/templates/build/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions app/templates/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
const Store = require('electron-store');

module.exports = new Store({
defaults: {
favoriteAnimal: '🦄'
}
});
4 changes: 4 additions & 0 deletions app/templates/editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions app/templates/gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions app/templates/gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn.lock
/dist
20 changes: 18 additions & 2 deletions app/templates/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ html,
body {
padding: 0;
margin: 0;
background: transparent;
}

/* Use OS default fonts */
body {
font-family: -apple-system, 'Helvetica Neue', Helvetica, sans-serif;
font-family: -apple-system,
BlinkMacSystemFont,
'Segoe UI',
Roboto,
Oxygen-Sans,
Ubuntu,
Cantarell,
'Helvetica Neue',
sans-serif,
'Apple Color Emoji',
'Segoe UI Emoji',
'Segoe UI Symbol';
text-rendering: optimizeLegibility;
font-feature-settings: 'liga', 'clig', 'kern';
}

header {
Expand All @@ -21,7 +36,8 @@ header {

header h1 {
font-size: 60px;
font-weight: 100;
font-weight: 200;
margin: 0;
padding: 0;
opacity: 0.7;
}
5 changes: 3 additions & 2 deletions app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
<html>
<head>
<meta charset="utf-8">
<title>Electron boilerplate</title>
<title><%= appName %></title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="container">
<header>
<h1>Electron boilerplate</h1>
<h1><%= appName %></h1>
<p></p>
</header>
<section class="main"></section>
<footer></footer>
Expand Down
84 changes: 65 additions & 19 deletions app/templates/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,75 @@
'use strict';
const electron = require('electron');
const path = require('path');
const {app, BrowserWindow, Menu} = require('electron');
/// const {autoUpdater} = require('electron-updater');
const {is} = require('electron-util');
const unhandled = require('electron-unhandled');
const debug = require('electron-debug');
const contextMenu = require('electron-context-menu');
const config = require('./config');
const menu = require('./menu');

const app = electron.app;
unhandled();
debug();
contextMenu();

// Adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();
// Note: Must match `build.appId` in package.json
app.setAppUserModelId('com.<%= githubUsername %>.<%= slugifiedAppName %>');

// Prevent window being garbage collected
let mainWindow;
// Uncomment this before publishing your first version.
// It's commented out as it throws an error if there are no published versions.
// if (!is.development) {
// const FOUR_HOURS = 1000 * 60 * 60 * 4;
// setInterval(() => {
// autoUpdater.checkForUpdates();
// }, FOUR_HOURS);
//
// autoUpdater.checkForUpdates();
// }

function onClosed() {
// Dereference the window
// For multiple windows store them in an array
mainWindow = null;
}
// Prevent window from being garbage collected
let mainWindow;

function createMainWindow() {
const win = new electron.BrowserWindow({
const createMainWindow = async () => {
const win = new BrowserWindow({
title: app.getName(),
show: false,
width: 600,
height: 400
});

win.loadURL(`file://${__dirname}/index.html`);
win.on('closed', onClosed);
win.on('ready-to-show', () => {
win.show();
});

win.on('closed', () => {
// Dereference the window
// For multiple windows store them in an array
mainWindow = undefined;
});

await win.loadFile(path.join(__dirname, 'index.html'));

return win;
};

// Prevent multiple instances of the app
if (!app.requestSingleInstanceLock()) {
app.quit();
}

app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}

mainWindow.show();
}
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
if (!is.macos) {
app.quit();
}
});
Expand All @@ -39,6 +80,11 @@ app.on('activate', () => {
}
});

app.on('ready', () => {
mainWindow = createMainWindow();
});
(async () => {
await app.whenReady();
Menu.setApplicationMenu(menu);
mainWindow = await createMainWindow();

const favoriteAnimal = config.get('favoriteAnimal');
mainWindow.webContents.executeJavaScript(`document.querySelector('header p').textContent = 'Your favorite animal is ${favoriteAnimal}'`);
})();
Loading

0 comments on commit 1aa7226

Please sign in to comment.