Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
SunnySnail committed Nov 20, 2020
1 parent 0e69a8e commit 51496a5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/shell/ls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#! /usr/bin/env node

const fs = require('fs');
const path = require('path')
const chalk = require('chalk')
const log = console.log
let arr = []

fs.readdir(path.resolve(process.cwd()), { withFileTypes: true }, (err, files) => {
if (err) return

files.forEach((file) => {
if (file.isDirectory()) {
arr.push(chalk.cyan.bold(file.name))
} else {
arr.push(chalk.white(file.name))
}
})

log(arr.join(' '))
})
44 changes: 44 additions & 0 deletions src/shell/md5pic/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#! /usr/bin/env node
const path = require('path')
const fs = require('fs')
const { createHash } = require('crypto')

const argv = process.argv
const prefix = `http://cdn.fasionchan.com/course`
const tmp = argv[2]
const host = argv[3]

function md5 (data) {
return createHash('md5').update(data).digest('hex')
}

if (!tmp || !host) {
console.log('请输入两个参数')
return
}

const directory = path.resolve(__dirname, tmp)

try {
let txt = ''
const files = fs.readdirSync(directory, {
withFileTypes: true
})
files.forEach((file) => {
let filePath = path.resolve(directory, file.name)
let extname = path.extname(filePath)
let con = fs.readFileSync(filePath)
let md5num = md5(con)
let newFilePath = path.resolve(directory, md5num + extname)
fs.renameSync(filePath, newFilePath)
console.log(`${file.name} ===============> ${md5num}${extname}`)
txt += `![](${prefix}/${host}/${md5num}${extname})\n`
})

fs.writeFileSync('pic.md', txt, 'utf8')

} catch(err) {
console.log('失败了')
console.log(err)
}

70 changes: 70 additions & 0 deletions src/shell/nc/nc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#! /usr/bin/env node

const net = require('net');

const DEFAULT_PORT = 8888;
let argvs = process.argv
argvs.shift();
argvs.shift();
console.log(argvs);

if (argvs[0] === '-l') {
let port = argvs[1] || DEFAULT_PORT;
createNetServer(port);
}

if (argvs[0] === '-h') {
let host = argvs[1] || 'localhost';
let port = argvs[3] || DEFAULT_PORT;
createNetClient(host, port);
}

// process.stdin.on('readable', () => {
// let chunk;
// while((chunk === process.stdin.read()) !== null) {
// process.stdout.write(`数据:${chunk}`)
// }
// });

function createNetServer (port) {
let server = net.createServer((client) => {
console.log('client connect');

client.on('end', () => {
console.log('client stop connect');
})

client.write('hello from server\r\n');
client.pipe(client);
})

server.on('error', (err) => {
throw err;
})

server.listen(port, () => {
console.log('server listen port: ', port);
})
}

function createNetClient(host ,port) {
let client = net.createConnection({
host,
port
}, () => {
console.log('连接服务器');
client.write('你好\r\n')
})

client.on('data', (data) => {
console.log(data.toString());
})

client.on('end', () => {
console.log('已从服务器断开');
})

client.on('error', () => {
console.log('fail');
})
}

0 comments on commit 51496a5

Please sign in to comment.