Skip to content

Commit

Permalink
Add support for script execution after creating a torrent
Browse files Browse the repository at this point in the history
  • Loading branch information
canibanoglu committed Nov 13, 2019
1 parent 4fec718 commit 8d01ae9
Show file tree
Hide file tree
Showing 5 changed files with 1,227 additions and 21 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node: 12.13.0-alpine

WORKDIR /usr/src/app
COPY . .
RUN npm install
RUN npm run build
RUN npm link

ENV CAN_TRACKER_ANNOUNCE_URL=http://localhost/announce
ENV CAN_TRACKER_PORT=32323
ENV CAN_TRACKER_PATH=/usr/src/app

45 changes: 41 additions & 4 deletions bin/createTorrent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node
import { checkEnvironmentVariables } from '../utils';
import { spawn } from 'child_process';
import { statSync } from 'fs';

checkEnvironmentVariables();

Expand All @@ -17,7 +19,8 @@ const {

const {
CAN_TRACKER_ANNOUNCE_URL,
CAN_TRACKER_WEB_SEED_URL
CAN_TRACKER_WEB_SEED_URL,
CAN_TRACKER_POST_CREATE_SCRIPT_PATH
} = process.env;

if (!CAN_TRACKER_ANNOUNCE_URL || !CAN_TRACKER_WEB_SEED_URL) {
Expand All @@ -39,6 +42,7 @@ async function writeTorrentFile(fileName, torrentBuffer) {
})
.then(() => {
console.info(`Successfully created .torrent file with name: ${fileName}`);
return filePath;
});
}

Expand All @@ -61,7 +65,40 @@ createTorrent(argv._, {
return Promise.all([
addTorrentToDatabase(parsedTorrent),
writeTorrentFile(torrentFileName, torrentBuffer)
]).then(() => {
]).then(([x, filePath]) => {
if (!CAN_TRACKER_POST_CREATE_SCRIPT_PATH) return;

})
});
try {
statSync(CAN_TRACKER_POST_CREATE_SCRIPT_PATH);
} catch (e) {
console.error("Post create script doesn't exist!");
process.exit(1);
}

console.log('Found post installation script in env');

let executor = null;

if (CAN_TRACKER_POST_CREATE_SCRIPT_PATH.endsWith('.js')) { executor = 'node'; }
else if (CAN_TRACKER_POST_CREATE_SCRIPT_PATH.endsWith('.sh')) { executor = 'bash'; }
else if (CAN_TRACKER_POST_CREATE_SCRIPT_PATH.endsWith('.zsh')) { executor = 'zsh'; }
else {
console.error('Only .js, .sh or .zsh scripts are supported.')
return;
}

const cp = spawn(executor, [CAN_TRACKER_POST_CREATE_SCRIPT_PATH, filePath, ...argv._]);

cp.stdout.on('data', data => {
console.log('post create script:', data.toString());
});

cp.stderr.on('data', data => {
console.error('post create script:', data.toString());
});

cp.on('close', code => {
console.log('post create script exited with code:', code);
});
});
});
33 changes: 17 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion scripts/wrapBinaryCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const WRAPPER_CONTENT = fileName => `#!/usr/bin/env node
const path = require('path');
require = require('esm')(module);
console.log(module.path);
module.exports = require(path.join(module.path, '..', '${fileName}'));
`;

Expand All @@ -26,4 +27,4 @@ export async function wrapBinaryCommands() {
Promise.all(promises).then(() => {
console.info('Successfully finished wrapping binary commands');
});
}
}
Loading

0 comments on commit 8d01ae9

Please sign in to comment.