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

Deploy via FTP #23

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "airbnb",
"extends": "airbnb/base",
"env": {
"browser": true,
"mocha": true,
Expand All @@ -10,6 +10,7 @@
"sinon": true
},
"rules": {
"no-console": 0,
"no-use-before-define": [2, "nofunc"],
"comma-dangle": [2, "always-multiline"],
"block-scoped-var": 0 // TEMPORARY until issue in ESLint is fixed
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,25 @@
"es6-promise": "^2.3.0",
"express": "^4.13.1",
"fs-extra": "^0.22.1",
"ftp-client": "^0.2.2",
"hidefile": "^1.1.0",
"lodash": "^3.10.0",
"minimatch": "^3.0.0",
"ngrok": "^0.1.99",
"prompt": "^0.2.14",
"q": "^1.4.1",
"recursive-readdir": "^1.2.1",
"s3": "^4.4.0",
"socket.io": "^1.3.6",
"watch": "^0.16.0",
"yargs": "^3.16.1",
"minimatch": "^3.0.0",
"hidefile": "^1.1.0"
"yargs": "^3.16.1"
},
"devDependencies": {
"babel": "^5.8.19",
"babel-eslint": "^4.0.5",
"babel-jscs": "^2.0.3",
"chai": "^3.2.0",
"eslint": "^0.24.1",
"eslint-config-airbnb": "0.0.7",
"eslint-plugin-react": "^3.1.0",
"eslint": "^1.0.0",
"eslint-config-airbnb": "1.0.0",
"jscs": "^2.0.0",
"mocha": "^2.2.5",
"rimraf": "^2.4.2",
Expand Down
56 changes: 48 additions & 8 deletions src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
Q = require('q'),
_ = require('lodash'),
s3 = require('s3'),
FtpClient = require('ftp-client'),
loginFile = path.join(process.cwd(), '.chcplogin');

module.exports = {
execute: execute
};

const deploymentModes = {
s3: uploadToS3,
ftp: uploadToFTP
};

function execute(context) {
var executeDfd = Q.defer();

Expand All @@ -25,9 +31,8 @@
}

function deploy(context) {
var executeDfd = Q.defer(),
config,
credentials;
var config,
loginInfo;

try {
config = fs.readFileSync(context.defaultConfig, 'utf-8');
Expand All @@ -42,19 +47,27 @@
process.exit(0);
}
try {
credentials = fs.readFileSync(loginFile, 'utf-8');
credentials = JSON.parse(credentials);
loginInfo = fs.readFileSync(loginFile, 'utf-8');
loginInfo = JSON.parse(loginInfo);
} catch(e) {
console.log('Cannot parse .chcplogin: ', e);
}
if(!credentials) {
if(!loginInfo) {
console.log('You need to run "cordova-hcp login" before you can run "cordova-hcp deploy".');
process.exit(0);
}

// console.log('Credentials: ', credentials);
// console.log('Config: ', config);
const pushMode = loginInfo.pushMode;
try {
return deploymentModes[pushMode](context, config, loginInfo[pushMode]);
} catch (e) {
console.error('unsupported deployment method ', e);
process.exit(0);
}
}

function uploadToS3(context, config, credentials) {
var executeDfd = Q.defer();
var client = s3.createClient({
maxAsyncS3: 20,
s3RetryCount: 3,
Expand Down Expand Up @@ -94,4 +107,31 @@
});
return executeDfd.promise;
}

function uploadToFTP(context, config, ftpConfig) {
const client = new FtpClient({
host: ftpConfig.host,
port: ftpConfig.port,
user: ftpConfig.username,
password: ftpConfig.password
}, {
logging: 'basic'
});

const executeDfd = Q.defer();

client.connect(function () {
client.upload(`${context.sourceDirectory}/**`, ftpConfig.path, {
baseDir: context.sourceDirectory,
overwrite: 'all'
}, function (result) {
if (!_.isEmpty(result.errors)) {
console.error('Some files could not be uploaded: ', result.errors);
return executeDfd.reject();
}
executeDfd.resolve();
});
});
return executeDfd;
}
})();
4 changes: 2 additions & 2 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const name = {
};

const s3bucket = {
description: 'Amazon S3 Bucket name (required for cordova-hcp deploy)',
description: 'Amazon S3 Bucket name (leave it blank to use an FTP)',
pattern: /^[a-zA-Z\-0-9\.]+$/,
message: 'Name must be only letters, numbers, or dashes',
};

const s3region = {
description: 'Amazon S3 region (required for chcp deploy)',
description: 'Amazon S3 region (leave it blank to use an FTP)',
pattern: /^(us-east-1|us-west-2|us-west-1|eu-west-1|eu-central-1|ap-southeast-1|ap-southeast-2|ap-northeast-1|sa-east-1)$/,
default: 'us-east-1',
message: 'Must be one of: us-east-1, us-west-2, us-west-1, eu-west-1, eu-central-1, ap-southeast-1, ap-southeast-2, ap-northeast-1, sa-east-1',
Expand Down
71 changes: 67 additions & 4 deletions src/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,39 @@ import { getInput, writeFile } from './utils';
const configFile = path.join(process.cwd(), 'cordova-hcp.json');
const loginFile = path.join(process.cwd(), '.chcplogin');

const schema = {
const ftpSchema = {
properties: {
host: {
description: 'Enter FTP host (required)',
message: 'FTP host',
required: true,
},
port: {
description: 'Enter FTP port',
message: 'FTP port',
required: false,
default: 21
},
path: {
description: 'Enter FTP path to your app',
message: 'FTP path',
required: true
},
username: {
description: 'Enter FTP username (required)',
message: 'FTP username',
required: true,
},
password: {
description: 'Enter FTP password (required)',
message: 'FTP password',
hidden: true,
required: true,
},
}
};

const s3Schema = {
properties: {
key: {
description: 'Amazon Access Key Id',
Expand All @@ -22,6 +54,18 @@ const schema = {
},
};

const loginSchema = {
properties: {
pushMode: {
description: 'Choose a method to push your code: (s3 | ftp)',
message: 'You need to choose a method to push your code',
required: true,
pattern: /(s3|ftp)/,
default: 's3'
}
},
};

export function execute(context) {
validateConfig();

Expand All @@ -30,9 +74,18 @@ export function execute(context) {
prompt.delimiter = ': ';
prompt.start();

getInput(prompt, schema)
.then(content => writeFile(loginFile, content))
.then(done);
getInput(prompt, loginSchema)
.then(res => getPushSchemaInput(pushModeToSchema(res.pushMode), res));
}

function getPushSchemaInput(schema, res) {
getInput(prompt, schema)
.then(content => ({
pushMode: res.pushMode,
[res.pushMode]: content
}))
.then(content => writeFile(loginFile, content))
.then(done);
}

function validateConfig() {
Expand Down Expand Up @@ -61,3 +114,13 @@ function done(err) {
console.log('You SHOULD add .chcplogin to your .gitignore');
console.log('( echo \'.chcplogin\' >> .gitignore )');
}

function pushModeToSchema(pushMode) {
switch (pushMode) {
case 'ftp':
return ftpSchema;
case 's3':
default:
return s3Schema;
}
}