Skip to content

Commit 1434cdb

Browse files
authored
Merge pull request #1190 from hackmdio/refactor
replace package with npm published version
2 parents ad9f29a + dc74a4c commit 1434cdb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2276
-3541
lines changed

.travis.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ env:
88

99
jobs:
1010
include:
11-
- env: task=npm-test
12-
node_js:
13-
- 6
14-
before_install:
15-
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version "$YARN_VERSION"
16-
- export PATH="$HOME/.yarn/bin:$PATH"
1711
- env: task=npm-test
1812
node_js:
1913
- 8

bin/manage_users

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,117 @@
11
#!/usr/bin/env node
22

33
// First configure the logger so it does not spam the console
4-
const logger = require("../lib/logger");
5-
logger.transports.forEach((transport) => transport.level = "warning")
4+
const logger = require('../lib/logger')
5+
logger.transports.forEach((transport) => {
6+
transport.level = 'warning'
7+
})
68

7-
const models = require("../lib/models/");
8-
const readline = require("readline-sync");
9-
const minimist = require("minimist");
9+
const models = require('../lib/models/')
10+
const readline = require('readline-sync')
11+
const minimist = require('minimist')
1012

11-
function showUsage(tips) {
12-
console.log(`${tips}
13+
function showUsage (tips) {
14+
console.log(`${tips}
1315
1416
Command-line utility to create users for email-signin.
15-
1617
Usage: bin/manage_users [--pass password] (--add | --del) user-email
17-
Options:
18-
--add Add user with the specified user-email
19-
--del Delete user with specified user-email
20-
--reset Reset user password with specified user-email
21-
--pass Use password from cmdline rather than prompting
22-
`);
23-
process.exit(1);
18+
Options:
19+
--add\tAdd user with the specified user-email
20+
--del\tDelete user with specified user-email
21+
--reset\tReset user password with specified user-email
22+
--pass\tUse password from cmdline rather than prompting
23+
`)
24+
process.exit(1)
2425
}
2526

26-
function getPass(argv, action) {
27-
// Find whether we use cmdline or prompt password
28-
if(typeof argv["pass"] !== 'string') {
29-
return readline.question(`Password for ${argv[action]}:`, {hideEchoBack: true});
30-
}
31-
console.log("Using password from commandline...");
32-
return argv["pass"];
27+
function getPass (argv, action) {
28+
// Find whether we use cmdline or prompt password
29+
if (typeof argv['pass'] !== 'string') {
30+
return readline.question(`Password for ${argv[action]}:`, { hideEchoBack: true })
31+
}
32+
console.log('Using password from commandline...')
33+
return argv['pass']
3334
}
3435

3536
// Using an async function to be able to use await inside
36-
async function createUser(argv) {
37-
const existing_user = await models.User.findOne({where: {email: argv["add"]}});
38-
// Cannot create already-existing users
39-
if(existing_user != undefined) {
40-
console.log(`User with e-mail ${existing_user.email} already exists! Aborting ...`);
41-
process.exit(1);
42-
}
43-
44-
const pass = getPass(argv, "add");
45-
46-
47-
// Lets try to create, and check success
48-
const ref = await models.User.create({email: argv["add"], password: pass});
49-
if(ref == undefined) {
50-
console.log(`Could not create user with email ${argv["add"]}`);
51-
process.exit(1);
52-
} else
53-
console.log(`Created user with email ${argv["add"]}`);
37+
async function createUser (argv) {
38+
const existingUser = await models.User.findOne({ where: { email: argv['add'] } })
39+
// Cannot create already-existing users
40+
if (existingUser !== undefined) {
41+
console.log(`User with e-mail ${existingUser.email} already exists! Aborting ...`)
42+
process.exit(1)
43+
}
44+
45+
const pass = getPass(argv, 'add')
46+
47+
// Lets try to create, and check success
48+
const ref = await models.User.create({ email: argv['add'], password: pass })
49+
if (ref === undefined) {
50+
console.log(`Could not create user with email ${argv['add']}`)
51+
process.exit(1)
52+
} else { console.log(`Created user with email ${argv['add']}`) }
5453
}
5554

5655
// Using an async function to be able to use await inside
57-
async function deleteUser(argv) {
58-
// Cannot delete non-existing users
59-
const existing_user = await models.User.findOne({where: {email: argv["del"]}});
60-
if(existing_user === undefined) {
61-
console.log(`User with e-mail ${argv["del"]} does not exist, cannot delete`);
62-
process.exit(1);
63-
}
64-
65-
// Sadly .destroy() does not return any success value with all
66-
// backends. See sequelize #4124
67-
await existing_user.destroy();
68-
console.log(`Deleted user ${argv["del"]} ...`);
56+
async function deleteUser (argv) {
57+
// Cannot delete non-existing users
58+
const existingUser = await models.User.findOne({ where: { email: argv['del'] } })
59+
if (existingUser === undefined) {
60+
console.log(`User with e-mail ${argv['del']} does not exist, cannot delete`)
61+
process.exit(1)
62+
}
63+
64+
// Sadly .destroy() does not return any success value with all
65+
// backends. See sequelize #4124
66+
await existingUser.destroy()
67+
console.log(`Deleted user ${argv['del']} ...`)
6968
}
7069

71-
7270
// Using an async function to be able to use await inside
73-
async function resetUser(argv) {
74-
const existing_user = await models.User.findOne({where: {email: argv["reset"]}});
75-
// Cannot reset non-existing users
76-
if(existing_user == undefined) {
77-
console.log(`User with e-mail ${argv["reset"]} does not exist, cannot reset`);
78-
process.exit(1);
79-
}
80-
81-
const pass = getPass(argv, "reset");
82-
83-
// set password and save
84-
existing_user.password = pass;
85-
await existing_user.save();
86-
console.log(`User with email ${argv["reset"]} password has been reset`);
71+
async function resetUser (argv) {
72+
const existingUser = await models.User.findOne({ where: { email: argv['reset'] } })
73+
// Cannot reset non-existing users
74+
if (existingUser === undefined) {
75+
console.log(`User with e-mail ${argv['reset']} does not exist, cannot reset`)
76+
process.exit(1)
77+
}
78+
79+
const pass = getPass(argv, 'reset')
80+
81+
// set password and save
82+
existingUser.password = pass
83+
await existingUser.save()
84+
console.log(`User with email ${argv['reset']} password has been reset`)
8785
}
8886

8987
const options = {
90-
add: createUser,
91-
del: deleteUser,
92-
reset: resetUser,
93-
};
88+
add: createUser,
89+
del: deleteUser,
90+
reset: resetUser
91+
}
9492

9593
// Perform commandline-parsing
96-
const argv = minimist(process.argv.slice(2));
94+
const argv = minimist(process.argv.slice(2))
9795

98-
const keys = Object.keys(options);
99-
const opts = keys.filter((key) => argv[key] !== undefined);
100-
const action = opts[0];
96+
const keys = Object.keys(options)
97+
const opts = keys.filter((key) => argv[key] !== undefined)
98+
const action = opts[0]
10199

102100
// Check for options missing
103101
if (opts.length === 0) {
104-
showUsage(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`);
102+
showUsage(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`)
105103
}
106104

107105
// Check if both are specified
108106
if (opts.length > 1) {
109-
showUsage(`You cannot ${action.join(' and ')} at the same time!`);
107+
showUsage(`You cannot ${action.join(' and ')} at the same time!`)
110108
}
111109
// Check if not string
112110
if (typeof argv[action] !== 'string') {
113-
showUsage(`You must follow an email after --${action}`);
111+
showUsage(`You must follow an email after --${action}`)
114112
}
115113

116114
// Call respective processing functions
117-
options[action](argv).then(function() {
118-
process.exit(0);
119-
});
115+
options[action](argv).then(function () {
116+
process.exit(0)
117+
})

lib/config/environment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const {toBooleanConfig, toArrayConfig, toIntegerConfig} = require('./utils')
3+
const { toBooleanConfig, toArrayConfig, toIntegerConfig } = require('./utils')
44

55
module.exports = {
66
sourceURL: process.env.CMD_SOURCE_URL,

lib/config/hackmdEnvironment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const {toBooleanConfig, toArrayConfig, toIntegerConfig} = require('./utils')
3+
const { toBooleanConfig, toArrayConfig, toIntegerConfig } = require('./utils')
44

55
module.exports = {
66
domain: process.env.HMD_DOMAIN,

lib/config/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
const crypto = require('crypto')
55
const fs = require('fs')
66
const path = require('path')
7-
const {merge} = require('lodash')
7+
const { merge } = require('lodash')
88
const deepFreeze = require('deep-freeze')
9-
const {Environment, Permission} = require('./enum')
9+
const { Environment, Permission } = require('./enum')
1010
const logger = require('../logger')
11-
const {getGitCommit, getGitHubURL} = require('./utils')
11+
const { getGitCommit, getGitHubURL } = require('./utils')
1212

1313
const appRootPath = path.resolve(__dirname, '../../')
1414
const env = process.env.NODE_ENV || Environment.development
@@ -17,7 +17,7 @@ const debugConfig = {
1717
}
1818

1919
// Get version string from package.json
20-
const {version, repository} = require(path.join(appRootPath, 'package.json'))
20+
const { version, repository } = require(path.join(appRootPath, 'package.json'))
2121

2222
const commitID = getGitCommit(appRootPath)
2323
const sourceURL = getGitHubURL(repository.url, commitID || version)
@@ -159,8 +159,8 @@ if (Object.keys(process.env).toString().indexOf('HMD_') !== -1) {
159159
if (config.sessionSecret === 'secret') {
160160
logger.warn('Session secret not set. Using random generated one. Please set `sessionSecret` in your config.js file. All users will be logged out.')
161161
config.sessionSecret = crypto.randomBytes(Math.ceil(config.sessionSecretLen / 2)) // generate crypto graphic random number
162-
.toString('hex') // convert to hexadecimal format
163-
.slice(0, config.sessionSecretLen) // return required number of characters
162+
.toString('hex') // convert to hexadecimal format
163+
.slice(0, config.sessionSecretLen) // return required number of characters
164164
}
165165

166166
// Validate upload upload providers

lib/config/oldEnvironment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const {toBooleanConfig} = require('./utils')
3+
const { toBooleanConfig } = require('./utils')
44

55
module.exports = {
66
debug: toBooleanConfig(process.env.DEBUG),

lib/history.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
// history
33
// external modules
4-
var LZString = require('lz-string')
4+
var LZString = require('@hackmd/lz-string')
55

66
// core
77
var config = require('./config')

lib/letter-avatars.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ exports.generateAvatarURL = function (name, email = '', big = true) {
3030
if (typeof email !== 'string') {
3131
email = '' + name + '@example.com'
3232
}
33-
name=encodeURIComponent(name)
33+
name = encodeURIComponent(name)
3434

3535
let hash = crypto.createHash('md5')
3636
hash.update(email.toLowerCase())
3737
let hexDigest = hash.digest('hex')
3838

3939
if (email !== '' && config.allowGravatar) {
40-
photo = 'https://www.gravatar.com/avatar/' + hexDigest;
40+
photo = 'https://www.gravatar.com/avatar/' + hexDigest
4141
if (big) {
4242
photo += '?s=400'
4343
} else {

lib/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
const {createLogger, format, transports} = require('winston')
2+
const { createLogger, format, transports } = require('winston')
33

44
const logger = createLogger({
55
level: 'debug',

lib/migrations/20160112220142-note-add-lastchange.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module.exports = {
1818

1919
down: function (queryInterface, Sequelize) {
2020
return queryInterface.removeColumn('Notes', 'lastchangeAt')
21-
.then(function () {
22-
return queryInterface.removeColumn('Notes', 'lastchangeuserId')
23-
})
21+
.then(function () {
22+
return queryInterface.removeColumn('Notes', 'lastchangeuserId')
23+
})
2424
}
2525
}

0 commit comments

Comments
 (0)