npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

execute-it

v1.1.1

Published

Execute file or code and return the result

Downloads

7

Readme

execute-it

执行文件并返回结果,目前已支持 .js.yml.yaml.json 格式的文件,同时还支持执行指定的源代码。

安装

$ npm install execute-it

API

getContent(filePath, ...props)

  • filePath,文件的绝对路径,支持 .js.yml.json 格式
  • ...props,当 filePath.js 文件,且该文件导出的是一个方法,则会将其透传给方法

通过 require 的方式,执行并获取文件中的内容。注意,该方法返回的是 Promise

const executeIt = require('execute-it');

executeIt.getContent(path.join(__dirname, '../fixtures/devops-use-function.js'), {
    group: 'ivweb-init',
    other: 'other'
})
    .then((data) => {
        console.log('result:',data)
    });

evaluateJSSourceTextModule(sourceOpts, ...props)

  • sourceOpts,参数,如果为字符串则为源码,等效于 sourceOpts.sourceText
    • sourceOpts.sourceText,js module 源码,必填
    • sourceOpts.packageContent,package.json 的内容,若 sourceText 有依赖第三方包,则需要配置进来
    • sourceOpts.NPM,默认值为 npm,你也可以指定为 cnpm 等,主要是搭配 sourceOpts.packageContent 使用,用于安装依赖
    • sourceOpts.doNotClear,是否在执行完成之后不要清理临时目录,默认值为 false,即会自动清理临时文件
    • sourceOpts.tmpDir,默认值为本组件下的 tmp 目录,例如 [xxx]/node_modules/execute-it/tmp
  • ...props,透传给 js module 的参数

场景一:如果 js 模块不依赖第三方包,则直接传递符合 CommonJS 规范的源码

import { evaluateJSSourceTextModule} from 'execute-it';

const code = `
 module.exports = function (name, opts) {
    return 'hello, ' + name + '! I am ' + opts.age;
};
`;

evaluateJSSourceTextModule(code, 'execute-it', { age: 123}).then(data => {
  // 输出: 'hello, execute-it! I am 123'
  console.log(data);
});

场景二:如果 js 模块依赖第三方包,则需要传递依赖

import { evaluateJSSourceTextModule} from 'execute-it';

const sourceText = `
const _ = require('lodash')
module.exports = function (name, opts) {
    return 'hello, ' + name + '! I am ' + JSON.stringify(_.merge({desc:'haha'},opts));
};
`;

const packageContent = `
{
  "dependencies": {
    "lodash": "^4.17.15"
  }
}
`;

evaluateJSSourceTextModule({ sourceText, packageContent}, 'execute-it', { age: 123}).then(data => {
  // 输出: 'hello, execute-it! I am {"desc":"haha","age":123}'
  console.log(data);
});

场景三:支持异步

import { evaluateJSSourceTextModule} from 'execute-it';

const sourceText = `
const axios = require('axios');

module.exports = async function (url) {
  const res = await axios.get(url);
  return res.status;
};
`;

const packageContent = `
{
  "dependencies": {
    "axios": "^0.19.2"
  }
}
`;

evaluateJSSourceTextModule({ sourceText, packageContent}, 'https://www.qq.com').then(data => {
  // 输出: 200
  console.log(data);
});