使用NodeJs处理文件系统的新手问题

时间:2016-03-02 17:11:51

标签: javascript node.js filesystems

这可能是一个虚拟问题,但我无法弄清楚为什么它不起作用。假设我打开一个终端 / home / romu / Musique / Alain Bashung / Compilation 文件夹来运行以下命令:

AtomicParsley "01. J'écume.m4a" --artwork ../../cover.jpg

结果是:

Started writing to temp file.
Progress: ======================================================> 99% -|
Finished writing to temp file.

所以它有效。现在,因为我想自动化这个过程,我正在写一个NodeJs脚本,这里是:

// ES6 only
'use strict';

// Get rid off the first 2 elements of the arguments array,
// First is the path of the node enfine itself
// second is the path of this script
let args = process.argv.slice(2);

// hard coded, will always be those values
let root = "/home/romu/Musique";
let ci = "../../cover.jpg";
let artist = args[0];
let album = args[1];

// Album path
let tracks_path = `${root}/${artist}/${album}`;

const spawn = require('child_process').spawn;
let ap = spawn('pwd', [], { cwd: tracks_path }); 
ap.stdout.on('data', (data) => {
  console.log(`Location: ${data}`);
});

ap = spawn('AtomicParsley', [`"01. J'écume.m4a" --artwork ../../cover.jpg`], { cwd: tracks_path }); 
ap.stdout.on('data', (data) => {
  console.log(`OUT: ${data}`);
});

ap.stderr.on('data', (data) => {
  console.log(`ERR: ${data}`);
});

ap.on('close', (code) => {
  console.log(`CODE: ${code}`);
});

我得到以下输出:

Location: /home/romu/Musique/Alain Bashung/Compilation
ERR: AtomicParsley error: can't open "01. J'écume.m4a" --artwork ../../cover.jpg for reading: No such file or directory
OUT: AP error trying to fopen "01. J'écume.m4a" --artwork ../../cover.jpg: No such file or directory
CODE: 1

我不知道发生了什么,为什么它不起作用。任何帮助都会很棒。感谢。

1 个答案:

答案 0 :(得分:1)

将参数作为数组传递,而不是完整的字符串。

ap = spawn('AtomicParsley', ["01. J'écume.m4a", "--artwork","../../cover.jpg"], { cwd: tracks_path });