Dockerode - custom folder for container - javascript

Since yesterday, I have been trying to make a separate folder for each container in the "folder" folder in node.js api using the docker Library called "dockerode". Unfortunately, I did not find any good solution that would work. I looked at the Pterodactyl Daemon (old) source code where they had it, but unfortunately it didn't work for me either. Do you know of any good solutions that could work well?
If you need any more info, I will write it for you here.
Have a nice rest of the day, Domi

Do you just want to create a temporary folder? If so you can just use the fs module:
const fs = require('fs');
exports.createTmpDir = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
};
exports.generateRandomString = (length = 15) => {
return Math.random().toString(36).substring(2, length);
};
you can use it like this:
// assume you call it from the root project directory
const root = ${process.cwd()}`;
// create a temp folder path. use this if you want to clean up later.
const tempDir = `${root}/tmp/${generateRandomString()}`;
createTmpDir(tempDir);
You can also use fs to copy or move your dockerfile in to that folder
Not sure if this answers your question or not?

Related

Is there any way to get a list of files in a directory into a variable when using webpack?

When creating a browser app, there doesn't seem to be any way to get a list of files in a particular location using javascript. In Nodejs this is easily done using fs.
Basically I have a directory called images/, and I want to manipulate a list of all the files as a javascript variable without having to manually create the list of files. I thought that since webpack bundles all of the files that there might be some way to generate the list as a JSON file (or an alternative) which could be read in with javascript -- though I haven't found any way to do that yet.
What is the best way to do this?
I was able to solve this by creating a plugin:
src/plugins/FileLister.js:
const fs = require('fs');
class FileLister {
constructor(options) {
if (!options || !options.path || !options.outputFile) {
const msg = "Please specify the path and outputFile options.";
throw new Error(msg);
}
this.options = options
}
apply(compiler) {
compiler.hooks.done.tap({name: 'FileLister'}, () => {
const files = fs.readdirSync(this.options.path)
fs.writeFileSync(this.options.outputFile, JSON.stringify(files));
})
}
}
module.exports = FileLister;
Then in my webpackage.config.js:
import:
const FileLister = require('./src/plugins/FileLister.js')
plugins section:
new FileeLister({
path: path.resolve(__dirname, 'src/important_files/'),
outputFile: path.resolve(__dirname, 'dist/important_files.json')
}),
This creates a file called important_files.json in the dist directory containing a list of the files in src/important_files/.

How to execute .js files within a folder based on their name ("number".js)

I've got a folder in my node.js project that contains multiple files, which I'd like to execute via a script. The names of the files are numbers just like '001.js', '002.js' and the script is meant to execute them in that order (from small to big) one after the other (asynchronous).
Unfortunately I can't really figure out a way to get this done the easy way.
Currently I'm importing all files into the script and execute them one after the other but I know that this isn't sustainable and kinda ridiculous...
Is there an easier way to achieve this result via a lean script?
This answer can help https://stackoverflow.com/a/2727191/829300
You can use 'fs' to read all files in a folder then require all.
Once you have got the array of file you can sort it
const fs = require('fs');
const testFolder = './folder/';
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
var x = require(testFolder + file)
console.log(x)
});
});

recreating gulp config on webpack

Right now really need help to understand webpack, although I'm trying so much, im still not understand how I can use webpack as a task runner rather than just module bundler. This is a learning process, so please lets not discuss of why gulp/webpack is better than other for this kind of job.
You see the code is actually pretty simple, the idea is to crawl every htmlfile based on a template that injected with a variable from css and js with the same name. This gulpfile is what I use in order to create Polymerjs Html File.
Is there any way I could convert this into Webpack specific build tools? Where should I put the information to crawl the folder in the webpack? Should I put it into index.js?
I understand the concept of using a specific entry files for Webpack, but when facing this kind of usecase, what should I do?
gulp.task('templates', function() {
// crawl folder pathToFolder
readdir(pathToFolder, ['*.scss', '*.js', '*.json'], function(err, files) {
// loop every file founds
files.forEach(function(file) {
let data = file.substr(10);
// make sure the path is correct when used on windows device.
data = data.replace(String.fromCharCode(92), String.fromCharCode(47));
let index = data.lastIndexOf('/');
let path = data.substr(0, index);
let scss = file.substr(0, file.lastIndexOf('.')) + '.scss';
let js = file.substr(0, file.lastIndexOf('.')) + '.js';
let json = file.substr(0, file.lastIndexOf('.')) + '.json';
// process all files into their respective loader and squash it into one variable to use on the template.
let process = {
css: '',
form: '',
js: '',
json: '',
};
if (fs.existsSync(scss)) {
process = Object.assign(process, {
css: sass.compiler.renderSync({
file: scss,
}).css,
});
}
if (fs.existsSync(js)) {
process = Object.assign(process, {
js: fs.readFileSync(js, 'utf8'),
});
}
if (fs.existsSync(json)) {
let x = gulp.src('./ramen-renderer.html')
.pipe(template({
json: json,
}));
process = Object.assign(process, {
json: x,
});
// jsonProcess = Object.assign(jsonProcess, {json: fs.readFileSync(json, "utf8")});
}
// render the final html path with gulp-template
return gulp.src(file)
.pipe(template(process))
.pipe(gulp.dest('src/' + path));
});
});
});
webpack is not a task runner but a module bundler. That means you need another way to run tasks. The most common way to do this is to use NPM for this. You define your tasks in your package.json under the scripts section.

Is it possible to require modules from outside of your project directory without relative paths?

I'm trying to build a local library of JS modules to use in Node projects.
If a new project lives in /Users/me/projects/path/to/new/project/ and my library files are located in /Users/me/projects/library/*.js is there a way to access those files without using a relative path?
In /Users/me/projects/path/to/new/project/app.js you can require foo.js like so:
var foo = require('../../../../../library/foo') and that will work but that's clunky and if files move you'd have to update your relative paths.
I've tried requireFrom and app-module-path with no luck as they are relative to a project root.
Any ideas for how to require files from outside of your project dir?
Thanks in advance!
var librarypath = '/Users/me/projects/library/';
// or if you prefer...
// var librarypath = '../../../../../library/';
var foo = require(librarypath + 'foo.js');
... or dressed up a bit more ...
function requirelib(lib){ return require('/Users/me/projects/library/'+lib+'.js'); }
var foo = requirelib('foo');
var bar = requirelib('bar');
I had the same problem many times. This can be solved by using the basetag npm package. It doesn't have to be required itself, only installed as it creates a symlink inside node_modules to your base path.
const localFile = require('$/local/file')
// instead of
const localFile = require('../../local/file')
Using the $/... prefix will always reference files relative to your apps root directory.
Disclaimer: I created basetag to solve this problem

rename templates folders with a gruntjs custom init task

I'm trying to create a custom init task for a personal template in Grunt.
This is the js which generate my new project after a grunt init:mytemplate
exports.description = 'Try Grunt';
exports.warnOn = '*';
exports.template = function(grunt, init, done) {
grunt.helper('prompt', {type: 'skin'}, [
grunt.helper('prompt_for', 'name', 'trygrunt'),
grunt.helper('prompt_for', 'title', 'Im Trying GruntJS'),
grunt.helper('prompt_for', 'author_name', 'Myself')
],
function(err, props) {
var files = init.filesToCopy(props);
init.copyAndProcess(files, props);
done();
});
};
Everything works fine: files and folder are correctly generated or renamed from the root folder of the custom template based on rename.json info.
The question is: how can i also dynamically rename folders and not only files?
i.e.
{
"libs/name.js": "libs/{%= name %}.js" //this works fine
"src/name": "src/{%= name %}" //this doesn't work
}
The init.filesToCopy method only looks at renames.json for specific file (not directory) matches when it first builds the files object. Your best bet is to programmatically modify the files object between init.filesToCopy and init.copyAndProcess.
This is possible by modifying the result of the init.filesToCopy object. However you need to modify the key rather than the value of each pair.
For example, I have a folder called lib/ that I wish to copy the contents into app/
var files = init.filesToCopy(props),
libFolder = 'app';
// Repath the files correctly
for (var file in files) {
if (file.indexOf('lib/') > -1) {
var path = files[file],
newFile = file.replace('lib/', libFolder + '/');
files[newFile] = path;
delete files[file];
}
}
init.copyAndProcess(files, props);
It's also worth noting that rename.json works on the lib/ value not the new folder.
One thing that I've done is to use the props.main value to extract the libFolder value (e.g. libFolder = props.main.split('/')[0])
Another way to accomplish this and get around the limitation of not being able to rename folders, is to 1) setup a grunt-contrib-copy task to copy the folders and files and apply whatever names you require to the new folders/files, and then 2) have a grunt-contrib-clean task clean out the old files/folders resulting in the same net effect as renaming the folders.

Categories

Resources