lib/cli/nodemcu-tool.js
async function upload(localFiles, options, onProgess){

    // the index of the current uploaded file
    let fileUploadIndex = 0;

    async function uploadFile(localFile, remoteFilename){

        // increment upload index
        fileUploadIndex++;

        // get file stats
        try{
            const stats = await _fs.statx(localFile);

            // check if file is directory
            if (stats.isDirectory()) {
                _mculogger.error('Path "' + localFile + '" is a directory.');
                return;
            }

        // local file available
        }catch (err){
            _logger.error('Local file not found "' + localFile + '" skipping...');
            _logger.debug(err);
            return;
        }

        // display filename
        _logger.log('Uploading "' + localFile + '" >> "' + remoteFilename + '"...');

        // normalize the remote filename (strip relative parts)
        remoteFilename = remoteFilename.replace(/\.\.\//g, '').replace(/\.\./g, '').replace(/^\.\//, '');

        // delete old file (may existent)
        await _nodeMcuConnector.remove(remoteFilename);

        // start file transfer
        await _nodeMcuConnector.upload(localFile, remoteFilename, options, function(current, total){
            // proxy and append file-number
            onProgess(current, total, fileUploadIndex);
        });

        // compile flag set ? and is a lua file ?
        if (options.compile && _path.extname(localFile).toLowerCase() == '.lua'){
            _mculogger.log(' |- compiling lua file..');

            await _nodeMcuConnector.compile(remoteFilename);
            _mculogger.log(' |- success');

            // drop original lua file
            await _nodeMcuConnector.remove(remoteFilename);
            _mculogger.log(' |- original Lua file removed');
        }
    }

    // try to establish a connection to the module
    await getConnection();

    // single file upload ?
    if (localFiles.length == 1){
        // extract first element
        const localFile = localFiles[0];

        // filename defaults to original filename minus path.
        // this behaviour can be overridden by --keeppath and --remotename options
        const remoteFile = options.remotename ? options.remotename : (options.keeppath ? localFile : _path.basename(localFile));

        // start single file upload
        await uploadFile(localFile, remoteFile);

        // log message
        _logger.log('File Transfer complete!');

        // run file ?
        if (options.run === true){
            await run(remoteFile);
        }

    // bulk upload ?
    }else{

        // file available ?
        while (localFiles.length > 0){
             // extract file
             const localFile = localFiles.shift();

             // keep-path option set ?
             const remoteFile = (options.keeppath ? localFile : _path.basename(localFile));

             // trigger upload
             await uploadFile(localFile, remoteFile);
        }

        // log message
        _logger.log('Bulk File Transfer complete!');
    }
}
async function upload(localFiles, options, onProgess){

    // the index of the current uploaded file
    let fileUploadIndex = 0;

    async function uploadFile(localFile, remoteFilename){

        // increment upload index
        fileUploadIndex++;

        // get file stats
        try{
            const stats = await _fs.statx(localFile);

            // check if file is directory
            if (stats.isDirectory()) {
                _mculogger.error('Path "' + localFile + '" is a directory.');
                return;
            }

        // local file available
        }catch (err){
            _logger.error('Local file not found "' + localFile + '" skipping...');
            _logger.debug(err);
            return;
        }

        // display filename
        _logger.log('Uploading "' + localFile + '" >> "' + remoteFilename + '"...');

        // normalize the remote filename (strip relative parts)
        remoteFilename = remoteFilename.replace(/\.\.\//g, '').replace(/\.\./g, '').replace(/^\.\//, '');

        // delete old file (may existent)
        await _nodeMcuConnector.remove(remoteFilename);

        // start file transfer
        await _nodeMcuConnector.upload(localFile, remoteFilename, options, function(current, total){
            // proxy and append file-number
            onProgess(current, total, fileUploadIndex);
        });

        // compile flag set ? and is a lua file ?
        if (options.compile && _path.extname(localFile).toLowerCase() == '.lua'){
            _mculogger.log(' |- compiling lua file..');

            await _nodeMcuConnector.compile(remoteFilename);
            _mculogger.log(' |- success');

            // drop original lua file
            await _nodeMcuConnector.remove(remoteFilename);
            _mculogger.log(' |- original Lua file removed');
        }
    }

    // try to establish a connection to the module
    await getConnection();

    // single file upload ?
    if (localFiles.length == 1){
        // extract first element
        const localFile = localFiles[0];

        // filename defaults to original filename minus path.
        // this behaviour can be overridden by --keeppath and --remotename options
        const remoteFile = options.remotename ? options.remotename : (options.keeppath ? localFile : _path.basename(localFile));

        // start single file upload
        await uploadFile(localFile, remoteFile);

        // log message
        _logger.log('File Transfer complete!');

        // run file ?
        if (options.run === true){
            await run(remoteFile);
        }

    // bulk upload ?
    }else{

        // file available ?
        while (localFiles.length > 0){
             // extract file
             const localFile = localFiles.shift();

             // keep-path option set ?
             const remoteFile = (options.keeppath ? localFile : _path.basename(localFile));

             // trigger upload
             await uploadFile(localFile, remoteFile);
        }

        // log message
        _logger.log('Bulk File Transfer complete!');
    }
}
async function upload ( localFiles , options , onProgess ) { // the index of the current uploaded file let fileUploadIndex = 0 ; async function uploadFile ( localFile , remoteFilename ) { // increment upload index fileUploadIndex ++ ; // get file stats try { const stats = await _fs . statx ( localFile ) ; // check if file is directory if ( stats . isDirectory ( ) ) { _mculogger . error ( 'Path "' + localFile + '" is a directory.' ) ; return ; } // local file available } catch ( err ) { _logger . error ( 'Local file not found "' + localFile + '" skipping...' ) ; _logger . debug ( err ) ; return ; } // display filename _logger . log ( 'Uploading "' + localFile + '" >> "' + remoteFilename + '"...' ) ; // normalize the remote filename (strip relative parts) remoteFilename = remoteFilename . replace ( / \.\.\/ / g , '' ) . replace ( / \.\. / g , '' ) . replace ( / ^\.\/ / , '' ) ; // delete old file (may existent) await _nodeMcuConnector . remove ( remoteFilename ) ; // start file transfer await _nodeMcuConnector . upload ( localFile , remoteFilename , options , function ( current , total ) { // proxy and append file-number onProgess ( current , total , fileUploadIndex ) ; } ) ; // compile flag set ? and is a lua file ? if ( options . compile && _path . extname ( localFile ) . toLowerCase ( ) == '.lua' ) { _mculogger . log ( ' |- compiling lua file..' ) ; await _nodeMcuConnector . compile ( remoteFilename ) ; _mculogger . log ( ' |- success' ) ; // drop original lua file await _nodeMcuConnector . remove ( remoteFilename ) ; _mculogger . log ( ' |- original Lua file removed' ) ; } } // try to establish a connection to the module await getConnection ( ) ; // single file upload ? if ( localFiles . length == 1 ) { // extract first element const localFile = localFiles [ 0 ] ; // filename defaults to original filename minus path. // this behaviour can be overridden by --keeppath and --remotename options const remoteFile = options . remotename ? options . remotename : ( options . keeppath ? localFile : _path . basename ( localFile ) ) ; // start single file upload await uploadFile ( localFile , remoteFile ) ; // log message _logger . log ( 'File Transfer complete!' ) ; // run file ? if ( options . run === true ) { await run ( remoteFile ) ; } // bulk upload ? } else { // file available ? while ( localFiles . length > 0 ) { // extract file const localFile = localFiles . shift ( ) ; // keep-path option set ? const remoteFile = ( options . keeppath ? localFile : _path . basename ( localFile ) ) ; // trigger upload await uploadFile ( localFile , remoteFile ) ; } // log message _logger . log ( 'Bulk File Transfer complete!' ) ; } }
upload a local file to nodemcu
upload a local file to nodemcu