Last active
August 29, 2015 14:02
-
-
Save katahirado/f68d14396ea6b8f634a7 to your computer and use it in GitHub Desktop.
callback版、Promise版
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
| JSFtp = require("jsftp") | |
| cheerio = require('cheerio') | |
| fs = require("fs") | |
| connectData = require("./ftp-setting-data") | |
| gui = window.require('nw.gui') | |
| tempFile = 'order.html' | |
| tempFilePath = "#{gui.App.dataPath}/#{tempFile}" | |
| getRemotePath = (path) -> | |
| if path then "#{path}/#{tempFile}" else tempFile | |
| upload = (callback) -> | |
| connect = connectData.getFTPSetting() | |
| new JSFtp( | |
| host: connect.host | |
| port: connect.port | |
| user: connect.account | |
| pass: connect.password | |
| ).put(tempFilePath, getRemotePath(connect.path), (err)-> | |
| if err then throw err else callback() | |
| ) | |
| fetch = (callback) -> | |
| connect = connectData.getFTPSetting() | |
| new JSFtp( | |
| host: connect.host | |
| port: connect.port | |
| user: connect.account | |
| pass: connect.password | |
| ).get(getRemotePath(connect.path), tempFilePath, (err)-> | |
| if err then throw err else callback() | |
| ) | |
| readTempFile = (callback) -> | |
| fs.readFile(tempFilePath, encoding: 'utf8',(err, data) -> | |
| if err then throw err else callback(data) | |
| ) | |
| module.exports.load = (callback) -> | |
| fetch(()-> | |
| readTempFile((data)-> | |
| $ = cheerio.load(data, decodeEntities: false) | |
| callback({ | |
| startDate: parseInt($('#start-date').html(), 10) | |
| endDate: parseInt($('#end-date').html(), 10) | |
| content: $('#order-body').html() | |
| }) | |
| ) | |
| ) | |
| module.exports.save = (object, callback) -> | |
| readTempFile((fileData)-> | |
| $ = cheerio.load(fileData, decodeEntities: false) | |
| $('#start-date').html('' + object.startDate) | |
| $('#end-date').html('' + object.endDate) | |
| $('#order-body').html(object.content.replace(/\r?\n/g, "<br />")) | |
| fs.writeFile(tempFilePath, $.html(), (err)-> | |
| if err then throw err else upload(callback) | |
| ) | |
| ) |
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
| JSFtp = require("jsftp") | |
| Promise = require("bluebird") | |
| cheerio = require('cheerio') | |
| fs = Promise.promisifyAll(require("fs")) | |
| connectData = require("./ftp-setting-data") | |
| gui = window.require('nw.gui') | |
| tempFile = 'order.html' | |
| tempFilePath = "#{gui.App.dataPath}/#{tempFile}" | |
| getRemotePath = (path) -> | |
| if path then "#{path}/#{tempFile}" else tempFile | |
| upload = () -> | |
| new Promise((resolve, reject)-> | |
| connect = connectData.getFTPSetting() | |
| new JSFtp( | |
| host: connect.host | |
| port: connect.port | |
| user: connect.account | |
| pass: connect.password | |
| ).put(tempFilePath, getRemotePath(connect.path), (err)-> | |
| if err then reject(err) else resolve() | |
| ) | |
| ) | |
| fetch = () -> | |
| new Promise((resolve, reject)-> | |
| connect = connectData.getFTPSetting() | |
| new JSFtp( | |
| host: connect.host | |
| port: connect.port | |
| user: connect.account | |
| pass: connect.password | |
| ).get(getRemotePath(connect.path), tempFilePath, (err)-> | |
| if err then reject(err) else resolve() | |
| ) | |
| ) | |
| module.exports.load = (callback) -> | |
| fetch().then(()-> | |
| fs.readFileAsync(tempFilePath, encoding: 'utf8') | |
| ).then( | |
| (data)-> | |
| $ = cheerio.load(data, decodeEntities: false) | |
| { | |
| startDate: parseInt($('#start-date').html(), 10) | |
| endDate: parseInt($('#end-date').html(), 10) | |
| content: $('#order-body').html() | |
| } | |
| ).then( | |
| (result)-> | |
| callback(result) | |
| ).error((err)-> | |
| console.error(err.message) | |
| ) | |
| module.exports.save = (object, callback) -> | |
| fs.readFileAsync(tempFilePath, encoding: 'utf8') | |
| .then( | |
| (fileData)-> | |
| $ = cheerio.load(fileData, decodeEntities: false) | |
| $('#start-date').html('' + object.startDate) | |
| $('#end-date').html('' + object.endDate) | |
| $('#order-body').html(object.content.replace(/\r?\n/g, "<br />")) | |
| return $.html() | |
| ).then( | |
| (data)-> | |
| fs.writeFileAsync(tempFilePath, data) | |
| ).then(upload).then(callback) | |
| .error((err)-> | |
| console.error(err.message) | |
| ) |
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
| fileLoader = require('./js/fileloader') | |
| $spinner = $('#spinner') | |
| #........................ | |
| $spinner.fadeIn() | |
| fileLoader.load((object)-> | |
| $startDate.data('DateTimePicker').setDate(object.startDate) | |
| $endDate.data('DateTimePicker').setDate(object.endDate) | |
| $('#content').val(object.content.replace(/<br>/g, "\n")) | |
| $spinner.fadeOut() | |
| ) | |
| #........................ | |
| $spinner.fadeIn() | |
| fileLoader.save(@$data,()-> | |
| #........................ | |
| if confirm('どのように表示されるか確認しますか?') | |
| location.href = "preview.html" | |
| else | |
| $spinner.fadeOut() | |
| ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment