mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 10:23:34 +00:00
Add link command-line
This commit is contained in:
parent
4686797ca3
commit
809580df97
121
bin/strapi-link.js
Normal file
121
bin/strapi-link.js
Normal file
@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
// Node.js core.
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const dns = require('dns');
|
||||
|
||||
// Public node modules.
|
||||
const _ = require('lodash');
|
||||
const request = require('request');
|
||||
const winston = require('winston');
|
||||
|
||||
// Logger.
|
||||
const logger = new (winston.Logger)({
|
||||
transports: [
|
||||
new (winston.transports.Console)({
|
||||
level: 'debug',
|
||||
colorize: 'level'
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
/**
|
||||
* `$ strapi link`
|
||||
*
|
||||
* Link an existing application to the Strapi Studio
|
||||
*/
|
||||
|
||||
module.exports = function () {
|
||||
const HOME = process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'];
|
||||
const pathToPackageJSON = path.resolve(process.cwd(), 'package.json');
|
||||
const appPkg = JSON.parse(fs.readFileSync(pathToPackageJSON));
|
||||
let invalidPackageJSON;
|
||||
|
||||
// First, check if we are in a Strapi project.
|
||||
try {
|
||||
require(pathToPackageJSON);
|
||||
} catch (err) {
|
||||
invalidPackageJSON = true;
|
||||
}
|
||||
|
||||
if (invalidPackageJSON) {
|
||||
logger.error('This command can only be used inside an Strapi project.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Check the internet connectivity.
|
||||
dns.resolve('google.com', function (noInternetAccess) {
|
||||
if (noInternetAccess) {
|
||||
logger.warn('No internet access...');
|
||||
logger.warn('Your application can not be linked to the Strapi Studio.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Read the `.strapirc` configuration file at $HOME.
|
||||
fs.readFile(path.resolve(HOME, '.strapirc'), 'utf8', function (noRcFile, config) {
|
||||
if (noRcFile) {
|
||||
logger.warn('You do not have a `.strapirc` file at `' + HOME + '`.');
|
||||
logger.warn('First, you need to create an account on http://studio.strapi.io/');
|
||||
logger.warn('Then, execute `$ strapi login` to start the experience.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Parse the config file.
|
||||
config = JSON.parse(config);
|
||||
|
||||
// Make sure the developer is logged in.
|
||||
if (_.isEmpty(config.email) || _.isEmpty(config.token)) {
|
||||
logger.error('You are not logged in.');
|
||||
logger.error('Execute `$ strapi login` to start the experience.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Create a new application on the Strapi Studio.
|
||||
request({
|
||||
method: 'POST',
|
||||
preambleCRLF: true,
|
||||
postambleCRLF: true,
|
||||
json: true,
|
||||
uri: 'http://studio.strapi.io/app',
|
||||
body: {
|
||||
name: appPkg.name,
|
||||
token: config.token
|
||||
}
|
||||
},
|
||||
|
||||
// Callback.
|
||||
function (err, res) {
|
||||
|
||||
// Log and exit if no internet access.
|
||||
if (err) {
|
||||
logger.warn('Impossible to access the Strapi Studio.');
|
||||
logger.warn('Your application is not linked to the Strapi Studio.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Parse the RC file.
|
||||
const currentJSON = JSON.parse(fs.readFileSync(path.resolve('config', 'global.json'), 'utf8'));
|
||||
const newJSON = JSON.stringify(_.merge(currentJSON, {studio: {appId: res.body.appId}}), null, ' ');
|
||||
|
||||
// Write the new `./config/global.json` with credentials.
|
||||
fs.writeFile(path.resolve('config', 'global.json'), newJSON, 'utf8', function (err) {
|
||||
if (err) {
|
||||
logger.error('Impossible to write the `appId`.');
|
||||
process.exit(1);
|
||||
} else {
|
||||
logger.info('Your application has successfully been linked to the Studio.');
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
@ -63,6 +63,12 @@ cmd.unknownOption = NOOP;
|
||||
cmd.description('open the Strapi framework console');
|
||||
cmd.action(require('./strapi-console'));
|
||||
|
||||
// `$ strapi link`
|
||||
cmd = program.command('link');
|
||||
cmd.unknownOption = NOOP;
|
||||
cmd.description('link an existing application to the Strapi Studio');
|
||||
cmd.action(require('./strapi-link'));
|
||||
|
||||
// `$ strapi config`
|
||||
cmd = program.command('config');
|
||||
cmd.unknownOption = NOOP;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user