Skip to content

Commit

Permalink
refactor: switch to basic-ftp (#148)
Browse files Browse the repository at this point in the history
* chore(ci): switch to GitHub Actions

* test: use ftp-srv

* chore: update dependencies
  • Loading branch information
stevenjoezhang authored Mar 28, 2024
1 parent f9580f1 commit 308b185
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 79 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/tester.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Tester

on: [push, pull_request]

jobs:
linter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
- name: Install Dependencies
run: npm install
- run: npm run eslint
- name: Coverage
run: npx nyc --reporter=lcovonly npm run test
env:
CI: true
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FTP deployer

[![Build Status](https://travis-ci.org/hexojs/hexo-deployer-ftpsync.svg?branch=master)](https://travis-ci.org/hexojs/hexo-deployer-ftpsync)
[![Build Status](https://img.shields.io/github/actions/workflow/status/hexojs/hexo-deployer-ftpsync/tester.yml?branch=master&label=test)](https://github.com/hexojs/hexo-deployer-ftpsync/actions?query=workflow%3ATester)
[![NPM version](https://badge.fury.io/js/hexo-deployer-ftpsync.svg)](https://www.npmjs.com/package/hexo-deployer-ftpsync)

Deploy your site via FTP.
Expand Down
47 changes: 1 addition & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,4 @@

/* global hexo */

const ftpsync = require('ftpsync');

hexo.extend.deployer.register('ftpsync', (args, callback) => {
if (!args.host || !args.user || args.pass == null) {
const help = [
'You should argsure deployment settings in _config.yml first!',
'',
'Example:',
' deploy:',
' type: ftpsync',
' host: <host>',
' port: [port] # Default is 21',
' remote: [remote] # Default is `/`',
' user: <user>',
' pass: <pass>',
' ignore: [ignore]',
' connections: [connections] # Default is 1',
'',
'For more help, you can check the docs: ' + 'http://hexo.io/docs/deployment.html'.underline
];

console.log(help.join('\n'));
return callback();
}

ftpsync.settings = {
local: hexo.public_dir,
host: args.host,
port: args.port || 21,
remote: args.remote || '/',
user: args.user,
pass: args.pass,
connections: args.connections || 1,
ignore: args.ignore || []
};

ftpsync.log.verbose = args.verbose || false;

if (ftpsync.settings.port > 65535 || ftpsync.settings.port < 1) {
ftpsync.settings.port = 21;
}

ftpsync.run((err, result) => {
if (err) callback(err);
});
});
hexo.extend.deployer.register('ftpsync', require('./lib/deployer'));
52 changes: 52 additions & 0 deletions lib/deployer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

const ftp = require('basic-ftp');

module.exports = async function(args, callback) {
const log = this.log;
if (!args.host || !args.user || args.pass == null) {
const help = [
'You should ensure deployment settings in _config.yml first!',
'',
'Example:',
' deploy:',
' type: ftpsync',
' host: <host>',
' user: <user>',
' pass: <pass>',
' remote: [remote] # Default is `/`',
' port: [port] # Default is 21',
' clear: [true|false] # Default is false',
' verbose: [true|false]',
'',
'For more help, you can check the docs: http://hexo.io/docs/deployment.html'
];

log.warn(help.join('\n'));
return callback();
}

const client = new ftp.Client();
client.ftp.verbose = args.verbose || false;

try {
await client.access({
host: args.host,
port: args.port || 21,
user: args.user,
password: args.pass,
secure: false
});

await client.ensureDir(args.remote || '/');
if (args.clear) await client.clearWorkingDir();
await client.uploadFromDir(this.public_dir);

log.info('Deployment complete');
} catch (err) {
log.error('FTP Deployment Error:', err);
} finally {
client.close();
callback();
}
};
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "hexo-deployer-ftpsync",
"version": "0.1.1",
"version": "0.2.0",
"description": "FTP deployer for Hexo",
"main": "index.js",
"files": [
"lib/",
"index.js"
],
"scripts": {
"eslint": "eslint .",
"eslint": "eslint index.js lib test",
"test": "mocha test/index.js",
"test-cov": "nyc npm run test"
},
Expand All @@ -25,16 +26,18 @@
"author": "Tommy Chen <[email protected]> (http://zespia.tw)",
"license": "MIT",
"dependencies": {
"ftpsync": "^0.2.0"
"basic-ftp": "^5.0.5"
},
"devDependencies": {
"chai": "^4.1.2",
"eslint": "^6.1.0",
"eslint-config-hexo": "^4.1.0",
"mocha": "^7.1.1",
"nyc": "^15.0.1"
"chai": "^4.3.7",
"eslint": "^8.57.0",
"eslint-config-hexo": "^5.0.0",
"ftp-srv": "^4.6.3",
"hexo-fs": "^4.1.1",
"mocha": "^10.4.0",
"nyc": "^15.1.0"
},
"engines": {
"node": ">=8.6.0"
"node": ">=14"
}
}
64 changes: 60 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
'use strict';

const should = require('chai').should(); // eslint-disable-line
const pathFn = require('path');
const fs = require('hexo-fs');
const FtpSrv = require('ftp-srv');

describe('hexo-deployer-ftpsync', () => {
// Tests.
it('should deploy through ftp.', () => {
return true;
describe('deployer', () => {
const baseDir = pathFn.join(__dirname, 'deployer_test');
const publicDir = pathFn.join(baseDir, 'public');
const fakeRemote = pathFn.join(baseDir, 'remote');

const hostname = '127.0.0.1';
const port = 8021;
const ftpServer = new FtpSrv({
url: `ftp://${hostname}:${port}`
});

ftpServer.on('login', ({ connection, username, password }, resolve, reject) => {
resolve({ root: fakeRemote });
});

ftpServer.listen();

const ctx = {
base_dir: baseDir,
public_dir: publicDir,
log: {
info: () => {},
error: () => {}
}
};

const deployer = require('../lib/deployer').bind(ctx);

before(() => {
fs.mkdir(baseDir);
return fs.writeFile(pathFn.join(publicDir, 'foo.txt'), 'foo');
});

beforeEach(() => {
});

after(() => {
ftpServer.close();
return fs.rmdir(baseDir);
});

afterEach(() => {
});

function validate() {
return fs.existsSync(pathFn.join(publicDir, 'foo.txt'));
}

it('default', () => {
return deployer({
host: '127.0.0.1',
user: 'anonymous',
pass: 'nopassword',
port
}, () => {}).then(() => {
return validate();
});
});
});

0 comments on commit 308b185

Please sign in to comment.