Code files
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
|
||||
const exists = (file) => new Promise((resolve, reject) => {
|
||||
fs.access(file, (err) => {
|
||||
if (err) {
|
||||
if (err.code !== 'ENOENT') { return reject(err) }
|
||||
return resolve({file, exists: false})
|
||||
}
|
||||
resolve({file, exists: true})
|
||||
})
|
||||
})
|
||||
|
||||
exists(process.argv[2])
|
||||
.then(({file, exists}) => console.log(`"${file}" does${exists ? '' : ' not'} exist`))
|
||||
.catch(console.error)
|
||||
@@ -0,0 +1,66 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const tableaux = require('tableaux')
|
||||
|
||||
const write = tableaux(
|
||||
{name: 'Name', size: 20},
|
||||
{name: 'Created', size: 30},
|
||||
{name: 'Inode', size: 10},
|
||||
{name: 'Mode', size: 8},
|
||||
{name: 'Lnks', size: 4},
|
||||
{name: 'Size', size: 6}
|
||||
)
|
||||
|
||||
function print(dir) {
|
||||
fs.readdirSync(dir)
|
||||
.map((file) => ({file, dir}))
|
||||
.map(toMeta)
|
||||
.forEach(output)
|
||||
write.newline()
|
||||
}
|
||||
|
||||
function toMeta({file, dir}) {
|
||||
const stats = fs.lstatSync(path.join(dir, file))
|
||||
let {birthtime, ino, mode, nlink, size} = stats
|
||||
birthtime = birthtime.toUTCString()
|
||||
mode = mode.toString(8)
|
||||
size += 'B'
|
||||
return {
|
||||
file,
|
||||
dir,
|
||||
info: [birthtime, ino, mode, nlink, size],
|
||||
isDir: stats.isDirectory(),
|
||||
isSymLink: stats.isSymbolicLink()
|
||||
}
|
||||
}
|
||||
|
||||
function output({file, dir, info, isDir, isSymLink}) {
|
||||
if (isSymLink) {
|
||||
outputSymlink(file, dir, info)
|
||||
return
|
||||
}
|
||||
write(file, ...info)
|
||||
if (!isDir) { return }
|
||||
const p = path.join(dir, file)
|
||||
write.arrow()
|
||||
fs.readdirSync(p).forEach((f) => {
|
||||
const stats = fs.lstatSync(path.join(p, f))
|
||||
const style = stats.isDirectory() ? 'bold' : 'dim'
|
||||
if (stats.isSymbolicLink()) { f = '\u001b[33m' + f + '\u001b[0m'}
|
||||
write[style](f)
|
||||
})
|
||||
write.newline()
|
||||
}
|
||||
|
||||
function outputSymlink(file, dir, info) {
|
||||
write('\u001b[33m' + file + '\u001b[0m', ...info)
|
||||
process.stdout.write('\u001b[33m')
|
||||
write.arrow(4)
|
||||
write.bold(fs.readlinkSync(path.join(dir, file)))
|
||||
process.stdout.write('\u001b[0m')
|
||||
write.newline()
|
||||
}
|
||||
|
||||
print(process.argv[2] || '.')
|
||||
@@ -0,0 +1 @@
|
||||
/tmp
|
||||
@@ -0,0 +1 @@
|
||||
my-symlink
|
||||
@@ -0,0 +1 @@
|
||||
my edit
|
||||
+1
@@ -0,0 +1 @@
|
||||
another-file
|
||||
@@ -0,0 +1 @@
|
||||
my-file
|
||||
@@ -0,0 +1 @@
|
||||
../meta.js
|
||||
@@ -0,0 +1 @@
|
||||
my-subdir/my-subsubdir/too-deep
|
||||
@@ -0,0 +1,26 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
const {execSync} = require('child_process')
|
||||
|
||||
const file = process.argv[2]
|
||||
if (!file) {
|
||||
console.error('specify a file')
|
||||
process.exit(1)
|
||||
}
|
||||
try {
|
||||
fs.accessSync(file)
|
||||
console.error('file already exists')
|
||||
process.exit(1)
|
||||
} catch (e) {
|
||||
makeIt()
|
||||
}
|
||||
|
||||
function makeIt() {
|
||||
const nobody = Number(execSync('id -u nobody').toString().trim())
|
||||
const fd = fs.openSync(file, 'w')
|
||||
fs.fchmodSync(fd, 0)
|
||||
fs.fchownSync(fd, nobody, nobody)
|
||||
console.log(file + ' created')
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
const {execSync} = require('child_process')
|
||||
|
||||
const file = process.argv[2]
|
||||
if (!file) {
|
||||
console.error('specify a file')
|
||||
process.exit(1)
|
||||
}
|
||||
try {
|
||||
fs.accessSync(file)
|
||||
console.error('file already exists')
|
||||
process.exit(1)
|
||||
} catch (e) {
|
||||
makeIt()
|
||||
}
|
||||
|
||||
function makeIt() {
|
||||
const nobody = Number(execSync('id -u nobody').toString().trim())
|
||||
fs.writeFileSync(file, '')
|
||||
fs.chownSync(file, nobody, nobody)
|
||||
fs.chmodSync(file, 0)
|
||||
console.log(file + ' created')
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const tableaux = require('tableaux')
|
||||
|
||||
const write = tableaux(
|
||||
{name: 'Name', size: 20},
|
||||
{name: 'Created', size: 30},
|
||||
{name: 'Inode', size: 10},
|
||||
{name: 'Mode', size: 8},
|
||||
{name: 'Lnks', size: 4},
|
||||
{name: 'Size', size: 6}
|
||||
)
|
||||
|
||||
function print(dir) {
|
||||
fs.readdirSync(dir)
|
||||
.map((file) => ({file, dir}))
|
||||
.map(toMeta)
|
||||
.forEach(output)
|
||||
write.newline()
|
||||
}
|
||||
|
||||
function toMeta({file, dir}) {
|
||||
const stats = fs.statSync(path.join(dir, file))
|
||||
let {birthtime, ino, mode, nlink, size} = stats
|
||||
birthtime = birthtime.toUTCString()
|
||||
mode = mode.toString(8)
|
||||
size += 'B'
|
||||
return {
|
||||
file,
|
||||
dir,
|
||||
info: [birthtime, ino, mode, nlink, size],
|
||||
isDir: stats.isDirectory()
|
||||
}
|
||||
}
|
||||
|
||||
function output({file, dir, info, isDir}) {
|
||||
write(file, ...info)
|
||||
if (!isDir) { return }
|
||||
const p = path.join(dir, file)
|
||||
write.arrow()
|
||||
fs.readdirSync(p).forEach((f) => {
|
||||
const stats = fs.statSync(path.join(p, f))
|
||||
const style = stats.isDirectory() ? 'bold' : 'dim'
|
||||
write[style](f)
|
||||
})
|
||||
write.newline()
|
||||
}
|
||||
|
||||
print(process.argv[2] || '.')
|
||||
@@ -0,0 +1 @@
|
||||
my edit
|
||||
@@ -0,0 +1 @@
|
||||
my-file
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "fetching-meta-data",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "meta.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "David Mark Clements",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tableaux": "^1.0.1"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user