-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Parse init #4960
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
Closed
Closed
Parse init #4960
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
70cfff2
Port bootstrap.sh into js script for npx
flovilmart 7a9c1f3
Adds folder creation
flovilmart 0fbae5b
New documentations
flovilmart eea3042
Better init
flovilmart b7b7f69
Adds interactive options and directory
flovilmart 7dd3d3e
Adds PORT to .env generated folder
flovilmart 54cd081
Merge branch 'master' into parse-init
acinader File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"no-console": "off" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
#!/usr/bin/env node | ||
|
||
const shell = require("shelljs"); | ||
const chalk = require("chalk"); | ||
const fs = require("fs"); | ||
const inquirer = require('inquirer'); | ||
const path = require('path'); | ||
const CWD = process.cwd(); | ||
const crypto = require('crypto'); | ||
const DEFAULT_MONGODB_URI = 'mongodb://127.0.0.1:27017/parse'; | ||
const CHECK = '✓'; | ||
const program = require('commander'); | ||
|
||
let useYarn = false; | ||
if (shell.which("yarn")) { | ||
useYarn = true; | ||
} | ||
|
||
function generateKey() { | ||
return crypto.randomBytes(16).toString('hex'); | ||
} | ||
|
||
function ok(message) { | ||
console.log(chalk.green(`${CHECK} ${message}`)); | ||
} | ||
|
||
async function getInstallationsDir({ directory, interactive }) { | ||
let target_directory; | ||
if (directory) { | ||
target_directory = directory; | ||
} else if (interactive) { | ||
const answer = await inquirer.prompt([ | ||
{ | ||
type: 'input', | ||
name: 'target_directory', | ||
message: 'Enter an installation directory', | ||
default: CWD, | ||
}, | ||
]); | ||
target_directory = answer.target_directory; | ||
} else { | ||
target_directory = './parse-server' | ||
} | ||
console.log(`This will setup parse-server in ${chalk.bold(target_directory)}`); | ||
await confirm(`Do you want to continue?`); | ||
console.log(`Setting up parse-server in ${chalk.bold(target_directory)}`); | ||
return target_directory; | ||
} | ||
|
||
function getAppConfiguration({ interactive }) { | ||
if (!interactive) { | ||
return { | ||
appName: 'My Parse Server', | ||
appId: generateKey(), | ||
masterKey: generateKey(), | ||
databaseURI: DEFAULT_MONGODB_URI | ||
}; | ||
} | ||
const questions = [ | ||
{ | ||
type: 'input', | ||
name: 'appName', | ||
message: 'Enter your Application Name', | ||
validate: (value) => { | ||
return value && value.length > 0 | ||
} | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'appId', | ||
message: 'Enter your Application Id (leave empty to generate)', | ||
default: generateKey(), | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'masterKey', | ||
message: 'Enter your Master Key (leave empty to generate)', | ||
default: generateKey(), | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'databaseURI', | ||
message: 'Enter your Database URL (valid mongodb or postgres)', | ||
default: DEFAULT_MONGODB_URI, | ||
} | ||
]; | ||
|
||
return inquirer.prompt(questions); | ||
} | ||
|
||
function confirm(message, defaults = true) { | ||
return inquirer.prompt([ | ||
{ | ||
type: 'confirm', | ||
name: 'continue', | ||
message: message, | ||
default: defaults, | ||
} | ||
]).then(result => { | ||
if (!result.continue) { | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
|
||
async function main({ | ||
directory, | ||
interactive, | ||
}) { | ||
let target_directory = await getInstallationsDir({ interactive, directory }); | ||
target_directory = path.resolve(target_directory); | ||
if (fs.existsSync(target_directory)) { | ||
console.log(chalk.red(`${chalk.bold(target_directory)} already exists.\naborting...`)); | ||
process.exit(1); | ||
} | ||
|
||
shell.mkdir(target_directory); | ||
|
||
const config = await getAppConfiguration({ interactive }); | ||
const { | ||
masterKey, | ||
databaseURI | ||
} = config; | ||
|
||
// Cleanup sensitive info | ||
delete config.masterKey; | ||
delete config.databaseURI; | ||
|
||
shell.cd(target_directory); | ||
|
||
const packageContent = { | ||
scripts: { | ||
start: "node -r dotenv/config node_modules/.bin/parse-server config.js" | ||
} | ||
}; | ||
fs.writeFileSync( | ||
target_directory + "/package.json", | ||
JSON.stringify(packageContent, null, 2) + '\n' | ||
); | ||
ok('Added package.json'); | ||
|
||
fs.writeFileSync( | ||
target_directory + '/config.js', | ||
'module.exports = ' + JSON.stringify(config, null, 2) + ';\n' | ||
); | ||
ok('Added config.js'); | ||
|
||
fs.writeFileSync( | ||
target_directory + '/.env', | ||
`PORT=1337\nPARSE_SERVER_MASTER_KEY=${masterKey}\nPARSE_SERVER_DATABASE_URI=${databaseURI}\n` | ||
) | ||
ok('Added .env'); | ||
|
||
shell.mkdir(target_directory + '/cloud'); | ||
ok('Created cloud/'); | ||
|
||
fs.writeFileSync(target_directory + '/cloud/main.js', `// Cloud Code entry point\n`); | ||
ok('Created cloud/main.js'); | ||
shell.mkdir(target_directory + '/public'); | ||
ok('Created public/'); | ||
|
||
if (useYarn) { | ||
shell.exec("yarn add parse-server dotenv"); | ||
} else { | ||
shell.exec("npm install parse-server dotenv --save"); | ||
} | ||
|
||
console.log(chalk.green(`parse-server is installed in \n\t${target_directory}!\n`)); | ||
await confirm(`Do you want to start the server now?\nEnsure a database running on ${databaseURI}`); | ||
if (useYarn) { | ||
shell.exec("yarn start"); | ||
} else { | ||
shell.exec("npm start"); | ||
} | ||
} | ||
|
||
program | ||
.option('-i, --interactive', 'Configure manually') | ||
.option('-d, --directory [directory]', 'The target directory where to create a new parse-server') | ||
.parse(process.argv); | ||
|
||
main(program); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.