Skip to content

Commit

Permalink
some updates from the github.dev ide
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-dspeed committed Apr 1, 2022
1 parent 2b9d0da commit 5a6d30f
Showing 1 changed file with 43 additions and 39 deletions.
82 changes: 43 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,79 @@
#!/usr/bin/env node
/* global process */
const { name } = require('./package.json');

const fs = require('fs');
const process = require('process');
const path = require('path');
const { basename, join } = require('path');
const Debug = require('debug');
const {
writeFileSync,
lstatSync,
readdirSync,
readFileSync,
existsSync,
mkdirSync
} = require('fs');

const { name } = JSON.parse(readFileSync('./package.json'));
const debug = Debug('FileTransfer::');

if (name === 'create-rollup' && process.cwd().indexOf('create-rollup') >-1 ) {
debug('Detected myself',process.cwd());
process.exit();
}
//if target is a directory a new file with the same name will be created
const normalizeTarget = (target, source) => (existsSync(target) && lstatSync(target).isDirectory())
? path.join(target, basename(source))
: target;

function copyFileSync( source, target ) {

var targetFile = target;

//if target is a directory a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}
const copyFileSync = (source, target) =>
!existsSync(target) && writeFileSync(target, readFileSync(source));

fs.writeFileSync(targetFile, fs.readFileSync(source));
}

function copyRecursiveSync( source, target ) {
var files = [];

//check if folder needs to be created or integrated
let targetPath = path.basename( source );
if (targetPath === 'create-rollup') {
targetPath = '';
debug([path.basename( source )].join());
let targetPath = (basename(source) === 'create-rollup') ? '' : basename(source);
if (targetPath === '') {
debug([basename( source )].join());
}
var targetFolder = path.join( target, targetPath); //


if (path.basename( source ) === 'node_modules' || path.basename( source ) === '.git') {
debug('targetFolder =>',targetFolder,path.basename( target));
debug('srcFolder =>',source,path.basename( target));
var targetFolder = path.join( target, targetPath);

if (basename( source ) === 'node_modules' || basename(source) === '.git') {
debug('targetFolder =>',targetFolder,basename( target));
debug('srcFolder =>',source,basename( target));
return;
}

debug('targetFolder =>',targetFolder);
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder );
if ( !existsSync( targetFolder ) ) {
mkdirSync( targetFolder );
}

//copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );


files.forEach( function ( file ) {
var curSource = path.join( source, file );

if (path.basename(curSource) === '.git') {
if ( lstatSync(source).isDirectory() ) {

readdirSync(source).forEach( ( file ) => {
var curSource = path.join( source, file );
if (basename(curSource) === '.git') {
return;
}

if (path.basename(curSource) === 'node_modules') {
if (basename(curSource) === 'node_modules') {
return;
}

debug('=>',curSource);
if ( fs.lstatSync( curSource ).isDirectory() ) {
copyRecursiveSync( curSource, targetFolder );
} else {
copyFileSync( curSource, targetFolder );
const normalizedTarget = normalizeTarget(targetFolder)
if (!existsSync(normalizedTarget)) {
copyFileSync( curSource, normalizedTarget );
} else {
console.warn(normalizedTarget, 'got created already please delete')
}
}
} );
});
}
}

Expand Down

0 comments on commit 5a6d30f

Please sign in to comment.