Skip to content

Commit

Permalink
Revert "feat(iOS): execute amplify-app flow on init and `codegen…
Browse files Browse the repository at this point in the history
… models` (#5917)" (#5960)

This reverts commit c47c8f7.
  • Loading branch information
jhockett authored Nov 25, 2020
1 parent 905f9c2 commit cd7951a
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 572 deletions.
8 changes: 1 addition & 7 deletions packages/amplify-app/bin/amplify-app
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
#!/usr/bin/env node
const args = require('yargs').argv;
const path = args.path;
const platform = args.platform;
const jsFramework = args.framework;
const skipEnvCheck = false;

require('../src/index.js').run({ path, platform, jsFramework, skipEnvCheck });
require('../src/index.js').run();
171 changes: 75 additions & 96 deletions packages/amplify-app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const inquirer = require('inquirer');
const emoji = require('node-emoji');
const { spawnSync, spawn } = require('child_process');
const frameworkConfigMapping = require('./framework-config-mapping');
const args = require('yargs').argv;
const { addAmplifyFiles } = require('./xcodeHelpers');
const ini = require('ini');
const semver = require('semver');
Expand All @@ -16,67 +17,45 @@ const amplify = /^win/.test(process.platform) ? 'amplify.cmd' : 'amplify';
const amplifyDev = /^win/.test(process.platform) ? 'amplify-dev.cmd' : 'amplify-dev';
const amplifyCliPackageName = '@aws-amplify/cli';

/**
* `amplify-app` entry point
*
* @param {Object} opts command options
* @param {string?} opts.path project path
* @param {string?} opts.platform ios | android | javascript
* @param {boolean?} opts.skipEnvCheck if true, skip environment checks (NodeJs version, amplify-cli)
* @param {string?} opts.jsFramework javascript framework
*
* @public
* @returns {Promise<void>}
*/
function run(opts) {
const projpath = opts.path;
function run() {
const projpath = args.path;

if (projpath) {
process.chdir(projpath);
}

if (!opts.skipEnvCheck) {
checkNodeVersion()
amplifyCLIVersionCheck()
.catch(e => {
console.error(e);
process.exit(1);
});
}

return createAmplifySkeletonProject(opts.platform, opts.jsFramework)
return checkNodeVersion()
.then(() => amplifyCLIVersionCheck())
.then(() => createAmplifySkeletonProject())
.then(frontend => createAmplifyHelperFiles(frontend))
.then(frontend => {
console.log(`${emoji.get('white_check_mark')} Amplify setup completed successfully.`);
console.log(`${emoji.get('boom')} Amplify setup completed successfully.`);
showHelpText(frontend);
process.exit(0);
})
.catch(e => {
console.error(e);
if (e) {
console.log(e);
}
process.exit(1);
});
}

/**
* Checks installed NodeJS version
* @private
*/
function checkNodeVersion() {
// Node version check
async function checkNodeVersion() {
const currentNodeVersion = process.versions.node;
const minNodeVersion = engines.node;
if (!semver.satisfies(currentNodeVersion, minNodeVersion)) {
const errorMsg = `You are running Node ${currentNodeVersion}.\n` +
`Amplify CLI requires Node ${minNodeVersion}. \n` +
`Please update your version of Node.`;
console.error(errorMsg);
console.error(
`You are running Node ${currentNodeVersion}.\n` +
`Amplify CLI requires Node ${minNodeVersion}. \n` +
`Please update your version of Node.`,
);
process.exit(1);
}
}

/**
* Installs Amplify CLI using npm
* @private
* @returns {Promise<void>}
*/
// Install CLI using npm
async function installAmplifyCLI() {
return new Promise((resolve, reject) => {
const amplifyCLIInstall = spawn(npm, ['install', '-g', amplifyCliPackageName], {
Expand All @@ -97,11 +76,7 @@ async function installAmplifyCLI() {
});
}

/**
* Check the amplify CLI version, install latest CLI if it does not exist or is too old
* @private
* @returns {Promise<void>}
*/
// Check the amplify CLI version, install latest CLI if it does not exist or is too old
async function amplifyCLIVersionCheck() {
const amplifyCLIVersionSpawn = spawnSync(amplify, ['-v']);
const minCLIVersion = engines['@aws-amplify/cli'];
Expand All @@ -123,15 +98,10 @@ async function amplifyCLIVersionCheck() {
}
}

/**
* Checks if amplify directory is present.
* If not - then generate a skeleton with a base project
* @private
* @param {string} platform
* @param {string} jsFramework
* @returns {Promise<void>}
*/
async function createAmplifySkeletonProject(platform, jsFramework) {
/* Check if amplify directory is present
If not - then generate a skeleton with a base project */

async function createAmplifySkeletonProject() {
if (!fs.existsSync('./amplify')) {
console.log(`${emoji.get('guitar')} Creating base Amplify project`);

Expand All @@ -145,11 +115,11 @@ async function createAmplifySkeletonProject(platform, jsFramework) {

createSkeletonAmplifyProject.on('exit', code => {
if (code === 0) {
return getProjectConfig(false, platform, jsFramework)
return getProjectConfig()
.then(projectConfig => {
const projectConfigFilePath = path.join('amplify', '.config', 'project-config.json');
fs.writeFileSync(projectConfigFilePath, JSON.stringify(projectConfig, null, 4));
console.log(`${emoji.get('white_check_mark')} Successfully created base Amplify Project`);
console.log(`${emoji.get('boom')} Successfully created base Amplify Project`);
resolve(projectConfig.frontend);
})
.catch(e => {
Expand All @@ -161,18 +131,16 @@ async function createAmplifySkeletonProject(platform, jsFramework) {
});
});
}
if (platform !== 'ios') {
console.log(
`An Amplify project is already initialized in your current working directory ${emoji.get('smiley')}. Not generating base project.\n`,
);
}

console.log(
`An Amplify project is already initialized in your current working directory ${emoji.get('smiley')}. Not generating base project.`,
);
console.log();
const existingApp = true;
const projectConfig = await getProjectConfig(existingApp, platform, jsFramework);
const projectConfig = await getProjectConfig(existingApp);
return projectConfig.frontend;
}

async function getProjectConfig(existingApp, argPlatform, argJsFramework) {
async function getProjectConfig(existingApp) {
if (existingApp === true) {
const projectConfigFilePath = path.join('amplify', '.config', 'project-config.json');
const projectConfig = JSON.parse(fs.readFileSync(projectConfigFilePath));
Expand All @@ -183,27 +151,21 @@ async function getProjectConfig(existingApp, argPlatform, argJsFramework) {
const projectName = path.basename(process.cwd());
let projectConfig = {};

const { suitableFrontend, jsFramework } = await guessPlatform(argPlatform, argJsFramework);
const { suitableFrontend, jsFrameWork } = await guessPlatform();

const projectConfigTemplateFilePath = path.join(__dirname, 'project-configs', `project-config-${suitableFrontend}.json`);
projectConfig = JSON.parse(fs.readFileSync(projectConfigTemplateFilePath));
projectConfig.projectName = projectName;

if (suitableFrontend === 'javascript') {
projectConfig.javascript.framework = jsFramework;
projectConfig.javascript.config = frameworkConfigMapping[jsFramework];
projectConfig.javascript.framework = jsFrameWork;
projectConfig.javascript.config = frameworkConfigMapping[jsFrameWork];
}

return projectConfig;
}

/**
* Guesses platform and used JavaScript framework if no values
* are provided
* @param {string?} providedPlatform optional platform
* @param {string?} providedJSFramework optional JavaScript framework
*/
async function guessPlatform(providedPlatform, providedJSFramework) {
async function guessPlatform() {
const frontendPlugins = {
javascript: 'amplify-frontend-javascript',
android: 'amplify-frontend-android',
Expand All @@ -212,15 +174,14 @@ async function guessPlatform(providedPlatform, providedJSFramework) {
};

let suitableFrontend;
let resolvedJSFramework = 'none';

const validFrontends = Object.keys(frontendPlugins);

if (providedPlatform) {
if (!validFrontends.includes(providedPlatform)) {
if (args.platform) {
if (!validFrontends.includes(args.platform)) {
throw new Error('Invalid platform value passed. Valid values are javascript/ios/android');
} else {
suitableFrontend = providedPlatform;
suitableFrontend = args.platform;
}
} else {
let fitToHandleScore = -1;
Expand All @@ -235,19 +196,21 @@ async function guessPlatform(providedPlatform, providedJSFramework) {
});
}

let jsFrameWork = 'none';

if (suitableFrontend === 'javascript') {
const validJSFrameworks = Object.keys(frameworkConfigMapping);

if (providedPlatform) {
if (!validJSFrameworks.includes(providedJSFramework)) {
throw new Error('Invalid framework value passed. Valid values are angular/ember/ionic/react/react-native/vue/none');
if (args.platform) {
if (!validJSFrameworks.includes(args.framework)) {
throw new Error('Invalid framework value passed. Valid values are angular/ember/ionic/react/react-native/vue/none');
} else {
resolvedJSFramework = providedJSFramework;
jsFrameWork = args.framework;
}
} else {
resolvedJSFramework = guessFramework(process.cwd());
jsFrameWork = guessFramework(process.cwd());

if (resolvedJSFramework === 'none') {
if (jsFrameWork === 'none') {
const platformComfirmation = {
type: 'list',
name: 'platform',
Expand All @@ -267,7 +230,7 @@ async function guessPlatform(providedPlatform, providedJSFramework) {
};

const { framework } = await inquirer.prompt(frameworkComfirmation);
resolvedJSFramework = framework;
jsFrameWork = framework;
}
}
}
Expand All @@ -276,41 +239,41 @@ async function guessPlatform(providedPlatform, providedJSFramework) {
if (suitableFrontend) {
console.log(`$ Amplify project setup for ${suitableFrontend} platform`);
}
if (resolvedJSFramework) {
console.log(`${emoji.get('white_check_mark')} Framework detected: ${resolvedJSFramework}`);
if (jsFrameWork) {
console.log(`${emoji.get('white_check_mark')} Framework detected: ${jsFrameWork}`);
}

return { suitableFrontend, jsFramework: resolvedJSFramework };
return { suitableFrontend, jsFrameWork };
}

function guessFramework(projectPath) {
let framework = 'none';
let frameWork = 'none';
try {
const packageJsonFilePath = path.join(projectPath, 'package.json');

if (fs.existsSync(packageJsonFilePath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonFilePath));
if (packageJson && packageJson.dependencies) {
if (packageJson.dependencies.react) {
framework = 'react';
frameWork = 'react';
if (packageJson.dependencies['react-native']) {
framework = 'react-native';
frameWork = 'react-native';
}
} else if (packageJson.dependencies['@angular/core']) {
framework = 'angular';
frameWork = 'angular';
if (packageJson.dependencies['ionic-angular']) {
framework = 'ionic';
frameWork = 'ionic';
}
} else if (packageJson.dependencies.vue) {
framework = 'vue';
frameWork = 'vue';
}
}
}
} catch (e) {
console.log(e.stack);
framework = 'none';
frameWork = 'none';
}
return framework;
return frameWork;
}

async function createJSHelperFiles() {
Expand Down Expand Up @@ -469,6 +432,10 @@ async function showHelpText(frontend) {
if (frontend === 'android') {
await showAndroidHelpText();
}

if (frontend === 'ios') {
await showIOSHelpText();
}
}

async function showJSHelpText() {
Expand All @@ -491,4 +458,16 @@ async function showAndroidHelpText() {
console.log('');
}

async function showIOSHelpText() {
console.log();
console.log(chalk.green('Some next steps:'));
console.log(
'Setting "modelgen" to true in amplifytools.xcconfig will allow you to generate models/entities for your GraphQL models in your next xcode build',
);
console.log(
'Setting "push" to true in the amplifytools.xcconfig will build all your local backend resources and provision them in the cloud in your next xcode build',
);
console.log('');
}

module.exports = { run };
Loading

0 comments on commit cd7951a

Please sign in to comment.