.gitignore000066400000000000000000000000471517005715700130570ustar00rootroot00000000000000/build /node_modules /*.log /yarn.lock .npmignore000066400000000000000000000002351517005715700130650ustar00rootroot00000000000000/.idea /artifacts /build /Gemfile /_layouts /_site /_includes /test /node_modules /*.iml /*.ipr /*.iws /.travis.yml /.scrutinizer.yml /Gruntfile.js /assets .scrutinizer.yml000066400000000000000000000005471517005715700142560ustar00rootroot00000000000000checks: javascript: true filter: excluded_paths: - test/* - node_modules/* - build/* - docs/* build: environment: node: 7.8 dependencies: before: - npm install -g grunt-cli tests: override: - command: 'grunt' coverage: file: 'build/coverage/clover.xml' format: 'clover' .travis.yml000066400000000000000000000001321517005715700131730ustar00rootroot00000000000000language: node_js node_js: - "7" - "node" before_script: - npm install grunt-cli -g Gruntfile.js000066400000000000000000000004541517005715700133660ustar00rootroot00000000000000module.exports = (grunt) => { const builder = require(`corifeus-builder`); const loader = new builder.loader(grunt); loader.js({ replacer: { type: 'p3x', npmio: true, }, }); grunt.registerTask('default', builder.config.task.build.js); };LICENSE000066400000000000000000000021371517005715700120760ustar00rootroot00000000000000MIT License Copyright (c) 2017 Patrik Laszlo Corifeus http://patrikx3.tk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. README.md000066400000000000000000000045351517005715700123540ustar00rootroot00000000000000[//]: #@corifeus-header [![Build Status](https://travis-ci.org/patrikx3/tools.svg?branch=master)](https://travis-ci.org/patrikx3/tools) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/patrikx3/tools/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/patrikx3/tools/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/patrikx3/tools/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/patrikx3/tools/?branch=master) [![Trello](https://img.shields.io/badge/Trello-p3x-026aa7.svg)](https://trello.com/b/gqKHzZGy/p3x) [![NPM](https://nodei.co/npm/p3x-tools.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/p3x-tools/) ------ # NPM Tools ### Node Version Requirement ``` >=7.8.0 ``` The ```async``` and ```await``` keywords are required. # Description [//]: #@corifeus-header:end ## Install ```javascript npm install -g p3x-tools p3x-tools -h p3x-tools forr -h p3x-tools rmdirr -h p3x-tools unpublish -h ``` ## Examples ```javascript p3x-tools forr node_modules 'echo $FOUND; ls -all' p3x-tools unpublish --dry --username patrixk3 --search corifeus,p3x p3x-tools rmdirr --dry node_modules ``` **It is used for primarily Unix/BSD/Linux/OSX, using Bash.** * Functions * NPM find (recursive) * NPM remove resursive delete directories * NPM unpublish packages (able to keep only the latest version by minor version) * Major.Minor.Commit-Build * So instead * 4.1.55-44 * 4.1.54-33 * 4.0.53-32 * 4.0.52-30 * 1.3.44-23 * 1.3.40-20 * 1.3.28-10 * 1.3.22-9 * 1.3.21-8 * 1.2.20-7 * 1.1.10-5 * 1.0.9-5 * Becomes * 4.1.55-44 * 4.0.53-32 * 1.3.44-23 * 1.2.20-7 * 1.1.10-5 * 1.0.9-5 # Docs ## Unpublish Version: ```Major.Minor.Commit-Build``` 1. unpublish all, keep all last minor versions 1. corifeus - ```publish all``` 1. p3x-systemd-manager - ```ncu -a, publish``` 1. p3x-angular-compile - ```ncu -a, publish``` 1. p3x-aes-folder - ```ncu -a, publish``` 1. server-scripts - ```ncu -a``` 1. linux-defaults - ```ncu -a``` 1. corifeus - ```ncu -a``` [//]: #@corifeus-footer --- [**P3X-TOOLS**](https://patrikx3.github.com/tools) Build v1.1.21-2 on 5/9/2017, 8:49:10 PM by [Patrik Laszlo](http://patrikx3.tk) [//]: #@corifeus-footer:end _config.yml000066400000000000000000000000321517005715700132100ustar00rootroot00000000000000theme: jekyll-theme-hackerbin/000077500000000000000000000000001517005715700116365ustar00rootroot00000000000000bin/p3x.js000077500000000000000000000014671517005715700127210ustar00rootroot00000000000000#!/usr/bin/env node const commander = require('commander'); const utils = require('corifeus-utils'); const pkg = require(`../package.json`); const mz = require('mz'); const start = async() => { // command // required // [command] optional // [command ...] variable options commander .version(pkg.version) .usage('[options]') ; require('../src/command/rm') require('../src/command/npm') require('../src/command/for') require('../src/command/git') require('../src/command/github') /* const isInModule = await mz.fs.exists(`${__dirname}/../../node_modules`); if (!isInModule) { require('../src/command/pdf') } */ commander.parse(process.argv); if (!process.argv.slice(2).length) { commander.outputHelp(); } } start();package.json000066400000000000000000000022131517005715700133520ustar00rootroot00000000000000{ "name": "p3x-tools", "version": "1.1.31-23", "corifeus": { "prefix": "p3x-", "publish": true, "type": "p3x", "code": "Screw" }, "bin": { "p3x": "bin/p3x.js" }, "license": "MIT", "description": "Tools", "main": "index.js", "directories": { "test": "test" }, "scripts": { "test": "grunt" }, "repository": { "type": "git", "url": "git+https://github.com/patrikx3/tools.git" }, "keywords": [ "tools", "unpublish", "npm" ], "author": "Patrik Laszlo ", "bugs": { "url": "https://github.com/patrikx3/tools/issues" }, "homepage": "https://pages.corifeus.tk/tools", "devDependencies": { "corifeus-builder": "^1.7.552-27", "mz": "^2.6.0" }, "dependencies": { "commander": "^2.9.0", "corifeus-utils": "^1.1.134-24", "fs-extra": "^3.0.1", "github-api": "^3.0.0", "globby": "^6.1.0", "ini": "^1.3.4", "tmp-promise": "^1.0.3" }, "engines": { "node": ">=7.8.0" } } src/000077500000000000000000000000001517005715700116555ustar00rootroot00000000000000src/command/000077500000000000000000000000001517005715700132735ustar00rootroot00000000000000src/command/for.js000066400000000000000000000030561517005715700144230ustar00rootroot00000000000000// rmdirr dir dirs const commander = require('commander'); const fsExtra = require('fs-extra'); const utils = require('corifeus-utils'); commander .command('for ') .description(` Finds a list of directories (without start ./ and end /) recursively `) .option('-d, --dry', 'Do not actually remove packages, just show what it does') .action(async function (dir, command, options) { command = command.join(' '); const find = require('../find'); console.info(`Directory finding: ${dir}`) const paths = await find({ find: dir }); const dry = options.dry || false; if (dry) { console.info(`Dry, doesn't do anything, just shows what it does`) } const promises = []; paths.forEach(async (path) => { const generatedCommand = `bash -c ' pushd ${path} set -e export FOUND=${path} ${command} popd '`; if (dry) { console.info(`Dirs`, path) // console.log(generatedCommand); } else { const run = utils.childProcess.exec(generatedCommand); run.exec.stdout.on('data', (data) => { console.info(data); }); run.exec.stderr.on('data', (data) => { console.error(data); }); promises.push(run); } //console.info(`Path ${path}, Execute ${command}`) }) Promise.all(promises); }) ;src/command/git.js000066400000000000000000000033141517005715700144150ustar00rootroot00000000000000const commander = require('commander'); const utils = require('corifeus-utils'); const find = require('../find'); const path = require('path'); //p3x for .git 'cd ..;p3x git truncate' const commands = [ 'truncate', ] commander .command('git [plusCommands...]') .description(` The versioning is Major.Minor.Commit-Build If you omit the package name, it will use all. commands: ${commands.join(', ')} `) .option('-d, --dry', 'Do not actually remove packages, just show what it does') .action(async function (command, plusCommands, options) { switch(command) { case 'truncate': case 'renew': const truncate = require('../git').truncate; await truncate(options); break; case 'push': await utils.childProcess.exec(` git add . git commit -am 'p3x-robot-push' git push `, true) break; case 'eachpkg': case 'each': let paths = await find({ find: '.git', all: true, excludes: [ 'node_modules', ] }); paths = paths.map((dir) => { return path.dirname(dir) }) const promises = []; paths.forEach((path) => { promises.push(utils.childProcess.exec(` bash -c ' pushd ${path} set -e ${plusCommands.join(' ')} popd ' `, true)) }) Promise.all(promises); break; default: console.error(`Unknown command: ${command}`) } }) ; src/command/github.js000066400000000000000000000017671517005715700151260ustar00rootroot00000000000000const commander = require('commander'); const utils = require('corifeus-utils'); const find = require('../find'); const path = require('path'); const github = require('../github'); //p3x for .git 'cd ..;p3x git truncate' const commands = [ 'mirror', ] commander .command('github ') .description(` commands: ${commands.join(', ')} `) .option('-d, --dry', 'Do not actually remove packages, just show what it does') .option('-u, --user', 'The GitHub repo, default is patrikx3') .option('-g, --git ', 'The GIT repo, example is https://user:password@git.patrikx3.tk/') .action(async function (command, options) { const user = options.user || 'patrikx3'; const gitUrl = options.git || 'https://git.patrikx3.tk' ; switch(command) { case 'mirror': await github.mirror(user, gitUrl, options.dry) break; default: console.error(`Unknown command: ${command}`) } }) ; src/command/npm.js000066400000000000000000000050601517005715700144240ustar00rootroot00000000000000const commander = require('commander'); const utils = require('corifeus-utils'); const mz = require('mz'); const hasin = require('lodash/hasIn'); const hasPackage = async () => { } // unpublish commander .command('npm [packages...]') .description(` The versioning is Major.Minor.Commit-Build If you omit the package name, it will use all. commands: unpublish `) .option('-u, --username [username]', 'Author username, defaults to patrikx3', (val) => { return val.split(','); }) .option('-s, --search [term]', `Search for the given packages, the default is 'p3x,corifeus`, (val) => { return val.split(','); }) .option('-a, --all') .option('-d, --dry', 'Do not actually remove packages, just show what it does') .action(async function (command, packages, options) { let search = options.search; if (search === undefined) { search = ['p3x', 'corifeus'] } let username = options.username; if (username === undefined) { username = ['patrikx3']; } const dry = options.dry || false; const all = options.all || false; switch(command) { case 'update': if (await mz.fs.exists('./package.json')) { await utils.childProcess.exec(`ncu -a --loglevel verbose --packageFile package.json`, true) } break; case 'unpublish': const unpublish = require('../npm-unpublish'); await unpublish(username, search, packages, dry, all); break; case 'publish': if (await mz.fs.exists('./package.json')) { const pkg = require(`${process.cwd()}/package.json`); if (hasin(pkg, 'corifeus.publish') && pkg.corifeus.publish === true) { await utils.childProcess.exec(` grunt publish || true npm publish || true npm publish --registry https://registry.npmjs.org `, true) } else { console.info(`This package.json has not corifeus.publish = true`); } } else { console.info(`This directory has no package.json`); } break; case 'login': await utils.childProcess.exec(`npm login --registry https://registry.npmjs.org`, true) break; default: console.error(`Unknown command: ${command}`) } }) ; src/command/pdf.js000066400000000000000000000005261517005715700144050ustar00rootroot00000000000000/* const commander = require('commander'); // unpublish commander .command('pdf [file]') .description(` Parse a pdf file `) .option('-d, --dry', 'Do not actually remove packages, just show what it does') .action(async function (file, options) { const pdf = require('../pdf'); await pdf(file); }) ; */src/command/rm.js000066400000000000000000000015521517005715700142520ustar00rootroot00000000000000// rmdirr dir dirs const commander = require('commander'); const fsExtra = require('fs-extra'); commander .command('rm ') .description(` Delete the list of directories (without start ./ and end /) recursively `) .option('-d, --dry', 'Do not actually remove packages, just show what it does') .action(async function (dirs, options) { const find = require('../find'); const files = await find({ find: dirs, type: 'd' }); const dry = options.dry || false; if (dry) { console.info(`Dry, doesn't remove anything`) } const promises = []; files.forEach(async (file) => { if (!dry) { promises.push(fsExtra.remove(file)); } console.info(`Delete ${file}`) }) Promise.all(promises); }) ;src/find.js000066400000000000000000000037021517005715700131350ustar00rootroot00000000000000const utils = require('corifeus-utils'); const path = require('path'); const mz = require('mz'); const multi = async (options) => { await options.find.forEachAsync(async (findable) => { const resolved = path.resolve(options.root, findable); const finds = await mz.fs.exists(resolved); if (finds) { const stat = await mz.fs.stat(resolved); if (options.all || stat.isDirectory()) { options.results.push(resolved); } } const foundHit= await mz.fs.readdir(options.root) const foundHitPromises = []; await foundHit.forEachAsync(async (foundDir) => { const resolvedFoundDir = path.resolve(options.root, foundDir); const stat = await mz.fs.stat(resolvedFoundDir); if (!stat.isDirectory()) { return; } if (options.find.includes(foundDir) || options.excludes.includes(foundDir)) { return; } const newOptions = Object.assign({}, options) newOptions.root = resolvedFoundDir; foundHitPromises.push(multi(newOptions)) }) await Promise.all(foundHitPromises); }) return options.results; } module.exports = async (options) => { options.root = options.root || process.cwd(); options.results = options.results || []; console.log(`Options: ${JSON.stringify(options, null, 2)}`) if (!Array.isArray(options.find)) { options.find = [options.find]; } options.find = options.find.map(findable => { findable= findable.trim(); if (findable.startsWith('./')) { findable = findable.substr(2); } if (findable.endsWith('/')) { findable = findable.substr(0, findable.length - 1); } return findable; }) options.all = options.all || false; options.excludes = options.excludes || []; return await multi(options); }; src/git.js000066400000000000000000000023251517005715700130000ustar00rootroot00000000000000const utils = require('corifeus-utils'); const globby = require('globby'); const mz = require('mz'); const ini = require('ini'); const truncate = async (options) => { const command = `git config --global credential.helper 'cache --timeout 7200' git checkout --orphan temp git add -A git commit -am "p3x-robot" git branch -D master git branch -m master git push -f origin master` console.log(command); if (!options.dry) { await utils.childProcess.exec(command, true) } } const replaceGitSubmodules = async(root, user) => { const files = await globby(`${root}/**/.gitmodules`) await files.forEachAsync(async(file) => { console.info(`Found submodule: ${file}`) const string = (await mz.fs.readFile(file)).toString(); const iniFile = ini.parse(string); Object.keys(iniFile).forEach((key) => { const submodule = iniFile[key] submodule.url = `https://github.com/${user}/${submodule.path}`; }) const result = ini.stringify(iniFile); await mz.fs.writeFile(file, result); console.info(`Submodule: ${file}, replaced`) }) } module.exports.truncate = truncate; module.exports.replaceGitSubmodules = replaceGitSubmodules;src/github.js000066400000000000000000000035231517005715700135000ustar00rootroot00000000000000const GitHub = require('github-api'); const tmp = require('tmp-promise'); const git = require('./git'); const utils = require('corifeus-utils'); const list = async(user ) => { const gh = new GitHub(); const response = await gh.getUser(user).listRepos(); const repos = response.data return repos.filter(repo => repo.fork === false && repo.full_name === `${user}/${repo.name}`); } const mirror = async(user, gitUrl, dry) => { let tmpDir; try { tmpDir = await tmp.dir(); console.info(`User: ${user}`); console.info(`Git url: ${gitUrl}`); console.info(`Generate tmp file: ${tmpDir.path}`); const repos = await list(user); await repos.forEachAsync(async(repo) => { await utils.childProcess.exec(` git clone https://github.com/${user}/${repo.name} ${tmpDir.path}/github/${repo.name} git clone ${gitUrl}/${repo.name}.git ${tmpDir.path}/git/${repo.name} `, true) }) console.info('Remove all Git .git dirs and move to Github .git dirs'); await repos.forEachAsync(async(repo) => { await utils.childProcess.exec(` rm -rf ${tmpDir.path}/git/${repo.name}/.git mv ${tmpDir.path}/github/${repo.name}/.git ${tmpDir.path}/git/${repo.name}/ `, true) }) console.info('Move GIT to Github dir'); await utils.childProcess.exec(` rm -rf ${tmpDir.path}/github/ mv ${tmpDir.path}/git/ ${tmpDir.path}/github/ `, true) console.info('Move submodules to GitHub'); await git.replaceGitSubmodules(`${tmpDir.path}/github`, user); } catch(e ) { throw e; } finally { if (tmpDir) { console.info(`Cleanup tmp file ${tmpDir.path}`); if (!dry) { tmpDir.cleanup(); } } } } module.exports.list = list; module.exports.mirror = mirror;src/npm-unpublish.js000066400000000000000000000111351517005715700150150ustar00rootroot00000000000000const utils = require('corifeus-utils') const path = require('path') // npm deprecate ${repo}@${version} "It is deprecated" const exec = require('child_process').exec; const repo = process.argv[2]; const removeVersion = (repo, version, keptVersions, dry, all) => { const semVersion = version.split('.'); if (!all) { const keptVersion = `${semVersion[0]}.${semVersion[1]}` if (!keptVersions.hasOwnProperty(keptVersion)) { console.info(`Kept version ${version}`); keptVersions[keptVersion] = version; return; } } const command = `npm unpublish ${repo}@${version}`; if (dry) { console.info(`[DRY] Removed version ${version}`); console.info(command) return; } return new Promise((resolve, reject) => { const run = exec(command, (e, stdout, stderr) => { if (e) { return reject(e); } if (stderr !== '') { return reject(stderr); } console.info(`Removed version ${version}`); return resolve(stdout); }) run.stdout.on('data', (data) => { console.info(data); }); run.stderr.on('data', (data) => { console.error(data); }); }) } const findVersions = async (repo) => { return new Promise((resolve, reject) => { exec(`npm show ${repo} --json`, async(e, stdout, stderr) => { if (e) { console.error(e); reject(e); return; } const info = JSON.parse(stdout); const versions = info.versions.reverse(); console.info(`Versions:`, `${versions.length} versions`, versions); resolve(versions) }); }) } const removePackage = async (repo, dry, all) => { console.info(`Remove repo ${repo}`); const versions = await findVersions(repo); const keptVersions = {} if (Object.keys(versions).length > 1) { for(let version of versions) { await removeVersion(repo, version, keptVersions, dry, all); } } const keptVersionsArray = Object.values(keptVersions); const log = `${repo} Total: ${versions.length} Kept Versions: ${keptVersionsArray.length} Remained versions, ${keptVersionsArray.join(' , ')} `; console.info(log); return { versions: versions, kept: keptVersionsArray, log: log }; } module.exports = async (usernames, search, packages, dry, all) => { const isAll = packages.length === 0; console.info(` ---------------------------------------------- Unpublish ---------------------------------------------- Usernames: ${usernames.join(', ')} Search: ${search.join(', ')} Packages: ${isAll ? 'all' : packages.join(', ')} Dry: ${dry} `) const logs = []; const errorLogs = []; const promises = []; const createLog = (repo, dry) => { return new Promise(async(resolve, reject) => { try { const log = await removePackage(repo, dry, all); logs.push(log); resolve(); } catch(e) { reject(e); } }) } //https://registry.npmjs.org/-/v1/search?text=p3x if (isAll) { const searchPromises = []; search.forEach((term) => { searchPromises.push( utils.http.request(`https://registry.npmjs.org/-/v1/search?text=${term}`) ) }) const results = await Promise.all(searchPromises); // find let objects = []; results.forEach(result => { objects = objects.concat(result.body.objects) }) objects = objects .filter((obj) => { if (!obj.package.hasOwnProperty('author')) { errorLogs.push({ pkg: obj.package, log: `000-This package is invalid: ${obj.package.name} ${obj.package.version} ${JSON.stringify(obj.package, null, 2)} ` }) return false; } return usernames.includes(obj.package.author.username); }) objects.forEach((object) => { promises.push(createLog(object.package.name, dry)) }); } else { packages.forEach((pkg) => { promises.push(createLog(pkg, dry)) }) } await Promise.all(promises); logs.forEach((log) => { console.info(log.log); }) errorLogs.forEach((log) => { console.info(log.log); }) console.info(` Total: ${logs.length} Errors: ${errorLogs.length} `) return logs; } src/pdf.js000066400000000000000000000026451517005715700127730ustar00rootroot00000000000000//const pdfjs = require('pdfjs-dist'); //const fsExtra = require('fs-extra'); const mz = require('mz'); const pdf = async(file) => { const buffer = await mz.fs.readFile(file); const doc = await pdfjs.getDocument(buffer); var numPages = doc.numPages; console.log('# Document Loaded'); console.log('Number of Pages: ' + numPages); console.log(); const meta = await doc.getMetadata() console.log('# Metadata Is Loaded'); console.log('## Info'); console.log(JSON.stringify(meta.info, null, 2)); console.log(); if (meta.metadata) { console.log('## Metadata'); console.log(JSON.stringify(meta.metadata.metadata, null, 2)); console.log(); } for (var pageNum = 1; pageNum <= numPages; pageNum++) { const page = await doc.getPage(pageNum); console.log('# Page ' + pageNum); var viewport = page.getViewport(1.0 /* scale */); console.log('Size: ' + viewport.width + 'x' + viewport.height); console.log(); const content = await page.getTextContent(); // Content contains lots of information about the text layout and // styles, but we need only strings at the moment var strings = content.items.map(function (item) { return item.str; }); console.log('## Text Content'); console.log(strings.join(' ')); // console.log(content); } } module.exports = pdf;test/000077500000000000000000000000001517005715700120455ustar00rootroot00000000000000test/scripts/000077500000000000000000000000001517005715700135345ustar00rootroot00000000000000test/scripts/transform-git-modules.js000066400000000000000000000003561517005715700203400ustar00rootroot00000000000000#!/usr/bin/env node const path = '/tmp/tmp-22366RXowst8OEp2G/github'; const globby = require('globby') const mz = require('mz'); const ini = require('ini') const utils = require('corifeus-utils'); replaceGitSubmodules(path, 'patrikx3');tools.iml000066400000000000000000000011621517005715700127310ustar00rootroot00000000000000 tools.ipr000066400000000000000000000044611517005715700127470ustar00rootroot00000000000000 false false false tools.iws000066400000000000000000001476601517005715700127700ustar00rootroot00000000000000 promise glob await pass passw $PROJECT_DIR$/node_modules/corifeus-utils/src true DEFINITION_ORDER project true DIRECTORY false 1494763075056 file://$USER_HOME$/ramdisk/persistence/content/.p3x-ramdisk-link/Projects/patrikx3/tools/src/command/github.js 26