Skip to content

Commit

Permalink
Merge branch 'feature/refactoring' into 'master'
Browse files Browse the repository at this point in the history
Code quality

See merge request weitingTW/zh-converter!5
  • Loading branch information
weiting-tw committed Aug 25, 2020
2 parents 216a647 + ca61494 commit 04dca7a
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 14 deletions.
174 changes: 173 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,176 @@ node_modules/
# coverage folder
coverage/
# gitlab public folder
public/
public/

test/*.xml

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ program

if (!process.argv.slice(2).length) {
program.outputHelp();
process.exit();
throw new Error('argument is empty');
}

let toTraditional = !!!program.simplified;
let toTraditional = !program.simplified;
let filterExtname = '' || program.extname;

if (toTraditional) {
Expand Down
27 changes: 16 additions & 11 deletions zh-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@ const chineseConv = require('chinese-conv');
exports.run = (process, toTraditional = true, filterExtname = '') => {
fs.readdir(process.cwd(), (err, data) => {
data.filter((file) => {
if (filterExtname === '' || filterExtname === '*') return true;
return path.extname(file) === filterExtname;
}).map(async (file) => {
await this.convertFile(file, toTraditional);
return filterExtname === '' || filterExtname === '*'
? true
: path.extname(file) === filterExtname;
}).map(file => {
this.convertFile(file, toTraditional);
});
});
}

exports.convertFile = async (file, toTraditional, callback) => {
fs.readFile(file, 'utf-8', async (err, data) => {
fs.readFile(file, 'utf-8', (err, data) => {
try {
await fs.writeFile(
file,
toTraditional ? chineseConv.tify(data) : chineseConv.sify(data),
() => {
console.log(`%s ${file} be converted! success.`, "\x1b[37m");
});
doConvert(file, toTraditional, data);
} catch (error) {
console.log(`%s ${file} be converted fail !`, "\x1b[31m");
if (callback)
callback(`${file} be converted fail !`);
}
});
}

function doConvert(file, toTraditional, data) {
fs.writeFile(
file,
toTraditional ? chineseConv.tify(data) : chineseConv.sify(data),
() => {
console.log(`%s ${file} be converted! success.`, "\x1b[37m");
});
}

0 comments on commit 04dca7a

Please sign in to comment.